Product List Page

The Product List page is contained in the ProductList.aspx file. The user is brought to this page in response to a selection from the Menu user control in the Home page (Default.aspx). When you first install the application, the categories available are Communications, Deception, General, Munitions, Protection, Tools, and Travel. The Product List page displays all products in a selected category.

For performance, this page is cached and refreshes every 100 minutes (6,000 seconds).

When users go from the Home page to the Product List page, there are two HTML parameters that carry important values. The first is named CategoryID and contains the category ID with which the database can be queried for the products in the selected category. The second is named Selection, and this simply remembers the selection number in the Menu user control. The following URL shows a category ID of 14 and a selection of 0:

 ProductsList.aspx?CategoryID=14&selection=0 

The user interface object that's used to display the products is a Data List. The ProductsList.aspx file has an ItemTemplate that declaratively describes the data values that will be bound to the DataList object, as shown here:

[View full width]

<ItemTemplate> <table border="0" width="300"> <tr> <td width="25"> </td> <td width="100" valign="middle" align="right"> <a href='ProductDetails.aspx?productID=<%# DataBinder.Eval(Container.DataItem, graphics/ccc.gif "ProductID") %>'> <img src='/books/2/627/1/html/2/ProductImages/thumbs/<%# DataBinder.Eval(Container.DataItem, graphics/ccc.gif "ProductImage") %>' width="100" height="75" border="0"> </a> </td> <td width="200" valign="middle"> <a href='ProductDetails.aspx?productID=<%# DataBinder.Eval(Container.DataItem, graphics/ccc.gif "ProductID") %>'> <span > <%# DataBinder.Eval(Container.DataItem, "ModelName") %> </span> <br> </a> <span ><b>Special Price: </b> <%# DataBinder.Eval(Container.DataItem, "UnitCost", "{0:c}") %> </span> <br> <a href='AddToCart.aspx?productID=<%# DataBinder.Eval(Container. DataItem, graphics/ccc.gif "ProductID") %>'> <span ><font color="#9D0000"> <b>Add To Cart<b></font></span> </a> </td> </tr> </table> </ItemTemplate>

You should notice that within the DataList's ItemTemplate, an Add To Cart link has been implemented. When this is clicked, the product item is added to the user's shopping cart. The product ID is passed to the AddToCart.aspx page as an HTML parameter named ProductID.

RECOMMENDED PRACTICE: A similar functionality could have been added by using an ImageButton that posts back to the ProductsList.aspx page, but there are reasons for implementing it the way it's currently being done.

The AddToCart.aspx page can be used from two different places, thus giving better reuse of the code. The AddToCart.aspx page is called from the ProductsList.aspx, ProductDetails.aspx, and ProductSearch.aspx pages.

Posting back to the ProductsList.aspx page with different parameter values would defeat the page caching that's been implemented because there would need to be a cache entry with a unique parameter string for each page. So instead of posting back with different parameters, the post backs are made with the data not being contained in parameters.


The DataList object is bound to data in the Page_Load() method. The code is simple. It starts by parsing the CategoryID parameter into an integer. It then instantiates a ProductsDB object. The recordset is assigned to the DataList's DataSource property with a call to the ProductsDB.GetProducts() method (which takes an integer argument specifying the category ID). And last, a call to the DataList's DataBind() method finishes it up. The C# and VB source code can be seen in Listing 8.4.

Listing 8.4 The Page_Load() Method That Binds All Products in a Category to a DataList

C#

 void Page_Load(Object sender, EventArgs e) {   // Obtain categoryId from QueryString   int categoryId = Int32.Parse(Request.Params["CategoryID"]);   // Obtain products and databind to an asp:datalist control   IBuySpy.ProductsDB productCatalogue = new IBuySpy.ProductsDB();   MyList.DataSource = productCatalogue.GetProducts(categoryId);   MyList.DataBind(); } 

VB

 Sub Page_Load(ByVal sender As Object, ByVal e As EventArgs)   ' Obtain categoryId from QueryString   Dim categoryId As Integer = CInt(Request.Params("CategoryID"))   ' Obtain products and databind to an asp:datalist control   Dim productCatalogue As IBuySpy.ProductsDB = New IBuySpy.ProductsDB()   MyList.DataSource = productCatalogue.GetProducts(categoryId)   MyList.DataBind() End Sub 


ASP. NET Solutions - 24 Case Studies. Best Practices for Developers
ASP. NET Solutions - 24 Case Studies. Best Practices for Developers
ISBN: 321159659
EAN: N/A
Year: 2003
Pages: 175

flylib.com © 2008-2017.
If you may any questions please contact us: flylib@qtcs.net