Bind Parent/Child controls (in a cacading manner) using ObjectDataSource

by Jagadish Pulakhandam on 9/14/2011 10:29:49 AM
Rated 0 from 0 votes
Brief: This sample binds a DropDownList and a ListBox to two different ObjectDataSource controls. However, ListBox displays child data when a parent item is selected in DropDownList.
Posted to: Data Binding using ObjectDataSource in ASP.NET
Add to DiggAdd to del.icio.usAdd to FURLAdd to RedditAdd to YahooAdd to BlinklistAdd to GoogleAdd to ma.gnoliaAdd to ShadowsAdd to Technorati

Demonstrates the following:
  • Parent/Child (or Master/Detail) binding using two ObjectDataSource controls
  • DropDownList acting as parent (bound to categories)
  • ListBox acting as child (bound to products, of a selected category in DropDownList)
  • All of the above without code-behind

Screen shot:



Source Code:

01.namespace NorthwindDataAccess.Models
02.{
03.    public class Category
04.    {
05.        public int CategoryID { get; set; }
06.        public string CategoryName { get; set; }
07.    }
08.      
09.    public class Product
10.    {
11.        public int ProductID { get; set; }
12.        public string ProductName { get; set; }
13.        public int CategoryID { get; set; }
14.        public double UnitPrice { get; set; }
15.    }
16.}
17.  
18.namespace NorthwindDataAccess.Factories
19.{
20.    public class CategoryFactory
21.    {
22.        public static List<Models.Category> GetList()
23.        {
24.            DataTable dtResult = DbHelper.GetResultSet("SELECT CategoryID, CategoryName FROM Categories");
25.            return dtResult
26.                .AsEnumerable()
27.                .Select(dr => new Models.Category() { 
28.                    CategoryID = Convert.ToInt16(dr["CategoryID"]),
29.                    CategoryName = dr["CategoryName"].ToString()
30.                })
31.                .ToList();
32.        }
33.    }
34.      
35.    public class ProductFactory
36.    {
37.        public static List<Product> GetListByCategory(int CategoryID)
38.        {
39.            DataTable dtResult = DbHelper.GetResultSet("SELECT ProductID, ProductName, CategoryID, UnitPrice FROM Products WHERE CategoryID = " + CategoryID);
40.            return dtResult
41.                .AsEnumerable()
42.                .Select(dr => new Models.Product()
43.                {
44.                    ProductID = Convert.ToInt16(dr["ProductID"]),
45.                    ProductName = dr["ProductName"].ToString(),
46.                    CategoryID = Convert.ToInt16(dr["CategoryID"]),
47.                    UnitPrice = Convert.ToDouble(dr["UnitPrice"])
48.                })
49.                .ToList();
50.        
51.    }   
52.}
01.<%@ Page Language="C#" AutoEventWireup="true" CodeBehind="WebForm1.aspx.cs" Inherits="NorthwindWebSite.WebForm1" %>
02.  
03.<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
04.  
05.<html xmlns="http://www.w3.org/1999/xhtml">
06.<head runat="server">
07.    <title></title>
08.</head>
09.<body>
10.    <form id="form1" runat="server">
11.    <div>
12.      
13.        <asp:DropDownList ID="ddlCategories" runat="server" Width="200px" 
14.            AutoPostBack="True" DataSourceID="odsrcCategories" DataTextField="CategoryName" 
15.            DataValueField="CategoryID">
16.        </asp:DropDownList>
17.        <asp:ObjectDataSource ID="odsrcCategories" runat="server" 
18.            SelectMethod="GetList" TypeName="NorthwindDataAccess.Factories.CategoryFactory">
19.        </asp:ObjectDataSource>
20.        <br /><br />
21.        <asp:ListBox ID="lstProducts" runat="server" Height="300px" Width="200px" 
22.            DataSourceID="odsrcProducts" DataTextField="ProductName" 
23.            DataValueField="ProductID">
24.        </asp:ListBox>
25.      
26.        <asp:ObjectDataSource ID="odsrcProducts" runat="server" 
27.            SelectMethod="GetListByCategory" 
28.            TypeName="NorthwindDataAccess.Factories.ProductFactory">
29.            <SelectParameters>
30.                <asp:ControlParameter ControlID="ddlCategories" Name="CategoryID" 
31.                    PropertyName="SelectedValue" Type="Int32" />
32.            </SelectParameters>
33.        </asp:ObjectDataSource>
34.      
35.    </div>
36.    </form>
37.</body>
38.</html>



Join the .NET Code Central Community and join the discussion!
Signing-up is FREE and quick. Do it now, we want to hear your opinion
0

Rated 0 from 0 votes ( login  to rate)
DotnetKicks DotnetKicksDe DotNetShoutout

Attachments / Source Code
You need to Login or Join for FREE to download the following

Video/Screen Recording (may not have audio narration/annotations)
You need to Login or Join for FREE to download the following