ProductsList.aspx Page

Description:   The ProductsList page displays a list of products in the selected product category

Implementation Notes:   This page appears when a user clicks one of the product subcategory links in the menu that appears on the left side of most pages in Adventure Works Cycles. The link is not actually part of the page; instead it is part of the Menu user control ( _Menu.ascx ) that is incorporated into the page.

When users click a product subcategory, the user control calls the ProductsList.aspx page.  The ID of the subcategory to display is passed to this page in the query string. For example, if the user browse items in the Road Bike subcategory, the URL to display this page would be ProductsList.aspx?SubCategoryID=2). Note that there are also additional query arguments for preserving the state of the tree control during navigation to the clicked subcategory.

The ProductsList page logic is encapsulated entirely within its Page_Load event handler.  This event handler is called when the page is accessed by a browser client. 

Page_Load Event Handler:  The Page_Load event handler obtains the specified CategoryID parameter using the Params collection of the page's Request  object.  The Params collection contains all query string, form field, cookie, and server variables sent from a client during an HTTP request. This the typical API through which page developers access arguments when doing page to page navigation transfers.

After converting the SubCategoryID into an Integer, the Page_Load event handler creates an instance of the ProductDB class and calls its GetProducts method, passing it the product ID. This method internally uses the usp_ProductsBySubCategory stored procedure to fetch the product information from the Adventure Works Cycles database.

The product collection is displayed using a templated <asp:DataList> server control.  The DataList server control contains a user-defined ItemTemplate that describes what each item in the list should look like.  The data values returned from the ProductsDB.GetProducts() method are populated into the DataList by setting its Datasource property, and then calling its DataBind()  method.  When DataBind() is called, the DataList will iterate over the DataSource and render a copy of the ItemTemplate for each row, populating it data from the row.

Add to Cart:   Within the DataList's ItemTemplate, we have implemented an "Add To Cart" link that, when clicked, adds the product item to the user's shopping cart.  This link references the AddToCart.aspx  page, and passes the ProductID of the product to add as a dynamically-generated querystring argument to the hyperlink. 

Note that while it is possible to implement this functionality as an ImageButton that posts back to the ProductsList.aspx page, we choose to implement it this way for the following reasons:
  1. Better reuse of the AddToCart logic.  Adventure Works Cycles lets the user add products to his cart from the ProductsList.aspx, ProductDetails.aspx and SearchResults.aspx pages.  These pages share the logic in the AddToCart page.
  2. Better use of the Output Cache.  If each "AddToCart" link posted back to it's respective page, there would be many more output cache entries for the page -- one for each unique querystring value.
Peformance Notes: