.NET
  Home arrow .NET arrow Page 4 - Completing a Simple Storefront with LINQ
ASP Free Forums 
.NET  
ASP  
ASP Code  
ASP.NET  
ASP.NET Code  
BrainDump  
C#  
Code Examples  
Database  
Database Code  
IIS  
Microsoft Access  
MS SQL Server  
Visual Basic.NET  
Windows Scripting  
Windows Security  
XML  
ASP Web Hosting  
ASP.NET Web Hosting 
Dedicated Servers 
Moblin 
JMSL Numerical Library 
Windows Web Hosting
 
IBM® developerWorks 
Sun Developer Network 
Weekly Newsletter
 
Developer Updates  
Free Website Content 
 RSS  Articles
 RSS  Forums
 RSS  All Feeds
Write For Us Get Paid 
Request Media Kit
Contact Us 
Site Map 
Privacy Policy 
Support 
 USERNAME
 
 PASSWORD
 
 
  >>> SIGN UP!  
  Lost Password? 
.NET

Completing a Simple Storefront with LINQ
By: Peyton McCullough
  • Search For More Articles!
  • Disclaimer
  • Author Terms
  • Rating: 3 stars3 stars3 stars3 stars3 stars / 2
    2008-05-13

    Table of Contents:
  • Completing a Simple Storefront with LINQ
  • Markup
  • Building the Product Browse Page
  • Building the Product Browse Page, Continued

  • Rate this Article: Poor Best 
      ADD THIS ARTICLE TO:
      Del.ici.ous Digg
      Blink Simpy
      Google Spurl
      Y! MyWeb Furl
    Email Me Similar Content When Posted
    Add Developer Shed Article Feed To Your Site
    Email Article To Friend
    Print Version Of Article
    PDF Version Of Article
     
     
    ADVERTISEMENT


    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.

       · Hello, everyone. This is the last article on my series about creating a simple...
     

    .NET ARTICLES

    - More on Commands, Input and the WPF
    - Grouping and Aggregating When Querying LINQ ...
    - Commands, Input and the WPF
    - Keyboard and Ink Input with WPF
    - Mouse Input and the WPF
    - Input with Windows Presentation Foundation
    - Introducing LINQ with XML and Databases
    - An Introduction to LINQ
    - Querying LINQ to SQL: Basics
    - Completing a Simple Storefront with LINQ
    - Knowing Your Environment: the System.Environ...
    - Creating the Home Page for a Simple Storefro...
    - LINQ Quickly with Language Integrated Queries
    - Introducing LINQ to SQL Designer using Visua...
    - Beginning LINQ to SQL Using Visual Studio 20...





    © 2003-2008 by Developer Shed. All rights reserved. DS Cluster 4 hosted by Hostway