.NET
  Home arrow .NET arrow Page 3 - 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  
Silverlight  
Visual Basic.NET  
Windows Scripting  
Windows Security  
XML  
Mobile Linux 
App Generation ROI 
IBM® developerWorks 
ASP Web Hosting  
ASP.NET Web Hosting 
Windows Web Hosting
 
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: 4 stars4 stars4 stars4 stars4 stars / 3
    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


    (Page 3 of 4 )

    It's finally time to build the product browse page. Add a page called Browse.aspx. As before, be sure to select MasterPage.master as the master page.

    The plan is to create two ListBox controls and a ListView control. The first ListBox control will contain the top-level categories. When the user selects a top-level category (for example, Bikes), the second ListBox control will become populated with the appropriate subcategory (for example, Mountain Bikes). Then, when the user selects a subcategory, the ListView will become populated with products in that category.

    We'll start with the the top-level category ListBox, but we need a way to populate it. We could work in the code-behind file as we did before, but there is an easier solution here: LinqDataSource. We can drop it right onto the page and set the appropriate properties declaratively. To get the top-level categories, we simply need to get all the categories that have a null ParentProductCategoryID, which makes sense, since they are themselves parent categories. Querying with a LinqDataSource isn't hard at all. Place this into the Browse.aspx page (inside of the Content control, of course):


    <asp:LinqDataSource ID="CategorySource" 
    ContextTypeName="AdventureWorksDataContext"

     TableName="ProductCategories" Where="ParentProductCategoryID = null" runat="server">

    </asp:LinqDataSource>


    As you can see, it's not hard at all to use LinqDataSource. We simply specify the name of the data context, the name of the table, and the condition of the Where clause. Hooking this up to a ListBox control isn't difficult either:


    <asp:ListBox ID="CategoryList" DataSourceID="CategorySource" 
    DataTextField="Name"

     DataValueField="ProductCategoryID" AutoPostBack="true" 
    runat="server">

    </asp:ListBox>


    Note how the control automatically posts back.

    Now we need to create a second LinqDataSource and a second ListBox. The process is much the same as before, with one important difference: we need to get the value of CategoryList and work it into our query. We need to retrieve all of the ProductCategory entries whose ParentProductCategoryIDs match the value of CategoryList. LinqDataSource actually provides a neat way to do this: WhereParameters. We can automatically generate a Where clause based on, among other things, the value of a control. Here's how it's done:


    <asp:LinqDataSource ID="SubcategorySource" 
    ContextTypeName="AdventureWorksDataContext"

     TableName="ProductCategories" AutoGenerateWhereClause="true" 
    runat="server">

     <WhereParameters>

     <asp:ControlParameter ControlID="CategoryList" 
    Name="ParentProductCategoryID"

     DefaultValue="-1" Type="Int32" />

     </WhereParameters>

    </asp:LinqDataSource>


    Notice how we have DefaultValue set to -1. This way, if nothing is selected in CategoryList, nothing will be in the second ListBox. Also, take note of how AutoGenerateWhereClause is set to true. This is necessary! Otherwise, all WhereParameters will be ignored.

    Now we need to wire SubcategorySource. This works the same way as before:


    <asp:ListBox ID="SubcategoryList" 
    DataSourceID="SubcategorySource"

     DataTextField="Name" DataValueField="ProductCategoryID"

     AutoPostBack="true" runat="server">

    </asp:ListBox>


    Try the page out now. Selecting a top-level category should populate the second ListBox with the appropriate subcategories.

    More .NET Articles
    More By Peyton McCullough


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

    .NET ARTICLES

    - Building Applications with Windows Workflow ...
    - Building the Data and Business Layers Using ...
    - The Transformed XML Explorer in MFC
    - List Control and Property Grid with the MFC ...
    - Font, Shell and Masked Edit Controls for MFC
    - Color, Link and Image Editor Controls for M...
    - New Controls for MFC
    - The Windows Ribbon Framework
    - Markup Language for the Ribbon Framework
    - Visually Upgrade Your MFC Project
    - New Features for the Statusbar in MFC
    - Working with the Statusbar in MFC
    - Iron Speed Design v60 Review
    - Binary and XML Serialization
    - Using CrystalReportViewer to Display Crystal...





    © 2003-2009 by Developer Shed. All rights reserved. DS Cluster 3 Hosted by Hostway
    For more Enterprise Application Development news, visit eWeek