E-Commerce
<asp:Label ID=”PriceLabel” runat=”server” Text=’<%# Eval(“Price”, “{0:C}”) %>’></asp:Label>
</ItemTemplate>
</asp:DataList>
The first control, SqlDataSource1, is the one that sources your catalog. The details of what is for sale in the Wrox United web site are stored in the Products table. It contains the connection string enabling you to connect to the database, and also a SelectCommand attribute that specifies the SQL that will pluck the Name, Description, Price, ProductID, and the URL of the image from the Products table. The SqlDataSource on its own doesn’t display anything, though; it needs the DataList to do this for it.
The DataList contains three controls as well as the formatting needed to display your catalog correctly. You started by replacing the Label control contained for PictureURL with an Image button. You did this because a label would just have displayed the text URL of the image, such as ProductImages\ Item1.gif, if you hadn’t. Secondly, you chose an ImageButton in place of an image because you needed a link to the product item page. Rather than having to move or re-create this folder of product images, we have placed it in the web site folder already, and all you’ve had to do is reference it as follows:
Eval(“PictureURL”, “ProductImages\thumb_{0}”)
The data-binding expression has two parameters: the first is the name of the field from the Products table you want to bind to your image button and the second is the contents of the field itself (indicated by {0}). You altered the second parameter so that it pointed to the ProductImages folder. Then you placed “thumb_”, which indicates that you don’t want to use a full-sized image, but instead a thumbnail of the image for the catalog.
You used labels for the other two items in the catalog, because you only needed to display text, but for your price you changed the format so that it displayed it in currency format as $0.00, rather than just 0.0000. Last, you altered the layout so that it displayed the items in a horizontal grid format, four across in each row.
The Product Item Page
In our design discussion we talked about all the possible features you might want to add to a specific product page, but settled on only really needing an enhanced description of the product. Of course your item page shouldn’t contain any less information about the product than the catalog, so you will need to display the name, image, and price in addition to the description. You add that to your web site in the following Try It Out.
Try It Out |
Building a Product Page for the Catalog |
1.Go to Solution Explorer, right-click the top item in it, and select Add New Item. Add a new Web Form and call it WroxShopItem.aspx.
2.Once again you need the SqlDataSource and DataList controls. In Design View, drag a SqlDataSource control from the Data section of the Toolbox.
3.Click the Configure Data Source flyout and select the WroxUnitedConnectionString from the drop-down list in the Choose your Data Connection dialog that appears.
4.Click Next and, as with the previous Try It Out, select the Products table and select each of the items within it, as shown in Figure 13-13.