Completing a Simple Storefront with LINQ - Building the Product Browse Page, Continued
(Page 4 of 4 )
To finish everything, we just need to create a ListView control that lists the products in the selected category. This ListView will need to have its own LinqDataSource, which is created much the same way as the last one. Again, we need to automatically generate the Where clause based on the value of a control-this time, SubcategoryList:
<asp:LinqDataSource ID="ProductSource"
ContextTypeName="AdventureWorksDataContext"
TableName="Products" AutoGenerateWhereClause="true"
runat="server">
<WhereParameters>
<asp:ControlParameter ControlID="SubcategoryList"
Name="ProductCategoryID"
DefaultValue="-1" Type="Int32" />
</WhereParameters>
</asp:LinqDataSource>
Now we just need to create the ListView. This is created in the same way that we created the ListView on the main page:
<asp:ListView ID="ProductList" DataSourceID="ProductSource"
runat="server">
<LayoutTemplate>
<asp:PlaceHolder ID="itemPlaceholder" runat="server" />
</LayoutTemplate>
<ItemTemplate>
<div class="itemBoxSmall">
<img src="ProductPicture.aspx?id=<%#Eval("ProductID") %>"
class="productImage" alt=<%#Eval("Name") %> />
<strong><%#Eval("Name") %></strong>
<br />
Product Number: <%#Eval("ProductNumber") %><br />
List Price: <%#Eval("ListPrice", "{0:C}") %>
</div>
</ItemTemplate>
</asp:ListView>
Go ahead and try the page. Select a top-level category and then a subcategory. The subcategory's products should then appear. However, you'll probably notice something right away, especially if you pick a large category: the data isn't paged. Thankfully, this is quite easy to fix. We simply need to add a DataPager control, which, given a control, a page size, and a method of formatting, will automatically page the data:
<asp:DataPager ID="ProductPager" PagedControlID="ProductList"
PageSize="10"
runat="server">
<Fields>
<asp:NumericPagerField />
</Fields>
</asp:DataPager>
That's it!
Conclusion
If you've followed this walk through from start to finish, you should be comfortable using LINQ in ASP.Net Websites. We've created a basic storefront that takes advantage of the capabilities provided by LINQ using various methods. Of course, the Website we've created is very primitive, and LINQ has a lot more to offer.
From here, try to expand the storefront on your own. For example, here, we browse individual products. However, it may be more logical to browse by models. Then, when a user selects a model, he could be confronted with DropDownList controls asking for the desired color and size. You could even rework the Website to use the full Adventure Works Cycles database, rather than the light version, or you could add administrative features. Go ahead and try.
| DISCLAIMER: The content provided in this article is not warranted or guaranteed by Developer Shed, Inc. The content provided is intended for entertainment and/or educational purposes in order to introduce to the reader key ideas, concepts, and/or product reviews. As such it is incumbent upon the reader to employ real-world tactics for security and implementation of best practices. We are not liable for any negative consequences that may result from implementing any information covered in our articles or tutorials. If this is a hardware review, it is not recommended to open and/or modify your hardware. |