Bind DropDownList/ListBox to ObjectDataSource

by Jagadish Pulakhandam on 9/14/2011 10:06:22 AM
Rated 0 from 0 votes
Brief: Demonstrates on binding a DropDownList/ListBox to ObjectDataSource.
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:
  • Configure ObjectDataSource to a business method
  • Configure DropDownList to bind to ObjectDataSource (includes Display field and Value field)
  • Configure ListBox to bind to ObjectDataSource
  • Retrieve values of selected items in DropDownList/ListBox

Screen shot:



Source Code:

01.using System;
02.using System.Collections.Generic;
03.using System.Linq;
04.using System.Text;
05.  
06.namespace NorthwindDataAccess.Models
07.{
08.    public class Category
09.    {
10.        public int CategoryID { get; set; }
11.        public string CategoryName { get; set; }
12.    }
13.}

01.using System;
02.using System.Collections.Generic;
03.using System.Linq;
04.using System.Text;
05.  
06.using System.Data;
07.namespace NorthwindDataAccess.Factories
08.{
09.    public class CategoryFactory
10.    {
11.        public static List<Models.Category> GetList()
12.        {
13.            DataTable dtResult = DbHelper.GetResultSet("SELECT CategoryID, CategoryName FROM Categories");
14.            return dtResult
15.                .AsEnumerable()
16.                .Select(dr => new Models.Category() { 
17.                    CategoryID = Convert.ToInt16(dr["CategoryID"]),
18.                    CategoryName = dr["CategoryName"].ToString()
19.                })
20.                .ToList();
21.        }
22.    }
23.}

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.<html xmlns="http://www.w3.org/1999/xhtml">
05.<head runat="server">
06.    <title></title>
07.</head>
08.<body>
09.    <form id="form1" runat="server">
10.    <div>
11.        <asp:DropDownList ID="DropDownList1" runat="server" Width="200px" 
12.            AutoPostBack="True" DataSourceID="ObjectDataSource1" 
13.            DataTextField="CategoryName" DataValueField="CategoryID" 
14.            onselectedindexchanged="DropDownList1_SelectedIndexChanged">
15.        </asp:DropDownList>
16.        <br />
17.        <br />
18.        <asp:ListBox ID="ListBox1" runat="server" Height="300px" Width="200px" 
19.            AutoPostBack="True" DataSourceID="ObjectDataSource1" 
20.            DataTextField="CategoryName" DataValueField="CategoryID" 
21.            onselectedindexchanged="ListBox1_SelectedIndexChanged"></asp:ListBox>
22.        <br />
23.        <br />
24.        <asp:Label ID="lblMsg" runat="server"></asp:Label>
25.        <asp:ObjectDataSource ID="ObjectDataSource1" runat="server" 
26.            SelectMethod="GetList" TypeName="NorthwindDataAccess.Factories.CategoryFactory">
27.        </asp:ObjectDataSource>
28.    </div>
29.    </form>
30.</body>
31.</html>

01.using System;
02.using System.Collections.Generic;
03.using System.Linq;
04.using System.Web;
05.using System.Web.UI;
06.using System.Web.UI.WebControls;
07.  
08.namespace NorthwindWebSite
09.{
10.    public partial class WebForm1 : System.Web.UI.Page
11.    {
12.        protected void Page_Load(object sender, EventArgs e)
13.        {
14.  
15.        }
16.  
17.        protected void DropDownList1_SelectedIndexChanged(object sender, EventArgs e)
18.        {
19.            lblMsg.Text = "Selected Category Id: " + DropDownList1.SelectedValue;
20.        }
21.  
22.        protected void ListBox1_SelectedIndexChanged(object sender, EventArgs e)
23.        {
24.            lblMsg.Text = "Selected Category Id: " + ListBox1.SelectedValue;
25.        }
26.    }
27.}

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