.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  
Visual Basic.NET  
Windows Scripting  
Windows Security  
XML  
ASP Web Hosting  
ASP.NET Web Hosting 
Mobile Linux 
App Generation ROI 
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


    (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

    - Using CrystalReportViewer to Display Crystal...
    - Creating Summary .Net Crystal Reports
    - 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





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