Search - Articles
DevASP.NET for ASP.NET, VB.NET, XML and C# (C-Sharp) Developers Tuesday, March 03, 2009
Dev Articles
Search Directory
ASP.NET
VB.Net
C-Sharp
SQL Server
 

DropDownList SelectedIndexChanged method in ASP.NET

Author: Rudy Pinson
Download Source Code : 1294_Ddl_SelectedIndexChanged.zip

SelectedIndexChanged() method provides the facility to change the data of other control in the page on the basis of selected item in DropDownList.

 

 

Please note that I have used NORTHWIND Sample Database for below example. If you want to use NORTHWIND then you have to download it otherwise use any other database. Complete source code is available to download.
 
  1. Create a new Web Site in Visual Studio 2010 either in C# or VB.NET
  2. Add a Web Form to Web Site
  3. Add a DropDownList and a GridView to the page and change the Default.aspx code as below.

    <asp:DropDownList ID="ddlCategories" runat="server" AutoPostBack="True"
                onselectedindexchanged="ddlCategories_SelectedIndexChanged">
    </asp:DropDownList>
    <br />
    <asp:GridView ID="GridView1" runat="server">
    </asp:GridView>

     
  4.  Now write code below in code behind file.

    C#
     
    protected void Page_Load(object sender, EventArgs e) {
        if (!IsPostBack) {
            ddlCategories.DataSource = Categories();
            ddlCategories.DataValueField = "CategoryID";
            ddlCategories.DataTextField = "CategoryName";
            ddlCategories.DataBind();
        }
    }
     
    protected void ddlCategories_SelectedIndexChanged(object sender, EventArgs e) {
        int categoryID = Convert.ToInt32(ddlCategories.SelectedItem.Value);
     
        GridView1.DataSource = Products(categoryID);
        GridView1.DataBind();
    }
     
    private DataTable Categories() {
        string sql = "SELECT * FROM Categories";
        SqlConnection conn = new SqlConnection("Data Source=MyPC;Initial Catalog=C:\\SQL SERVER 2000 SAMPLE DATABASES\\NORTHWND.MDF;Integrated Security=True");
        conn.Open();
        SqlCommand cmd = new SqlCommand(sql, conn);
        SqlDataAdapter da = new SqlDataAdapter(cmd);
        DataTable dt = new DataTable();
        da.Fill(dt);
        conn.Close();
        return dt;
    }
     
    private DataTable Products(int categoryID) {
        string sql = "SELECT * FROM Products WHERE CategoryID =" + categoryID;
        SqlConnection conn = new SqlConnection("Data Source=MyPC;Initial Catalog=C:\\SQL SERVER 2000 SAMPLE DATABASES\\NORTHWND.MDF;Integrated Security=True");
        conn.Open();
        SqlCommand cmd = new SqlCommand(sql, conn);
        SqlDataAdapter da = new SqlDataAdapter(cmd);
        DataTable dt = new DataTable();
        da.Fill(dt);
        conn.Close();
        return dt;
    }
     
    VB.NET
     
    Protected Sub Page_Load(ByVal sender As Object, ByVal e As EventArgs) Handles Me.Load
        If Not IsPostBack Then
            ddlCategories.DataSource = Categories()
            ddlCategories.DataValueField = "CategoryID"
            ddlCategories.DataTextField = "CategoryName"
            ddlCategories.DataBind()
        End If
    End Sub
     
    Protected Sub ddlCategories_SelectedIndexChanged(ByVal sender As Object, ByVal e As EventArgs) Handles ddlCategories.SelectedIndexChanged
        Dim categoryID As Integer = Convert.ToInt32(ddlCategories.SelectedItem.Value)
     
        GridView1.DataSource = Products(categoryID)
        GridView1.DataBind()
    End Sub
     
    Private Function Categories() As DataTable
        Dim sql As String = "SELECT * FROM Categories"
        Dim conn As New SqlConnection("Data Source=MyPC;Initial Catalog=C:\SQL SERVER 2000 SAMPLE DATABASES\NORTHWND.MDF;Integrated Security=True")
        conn.Open()
        Dim cmd As New SqlCommand(sql, conn)
        Dim da As New SqlDataAdapter(cmd)
        Dim dt As New DataTable()
        da.Fill(dt)
        conn.Close()
        Return dt
    End Function
     
    Private Function Products(ByVal categoryID As Integer) As DataTable
        Dim sql As String = "SELECT * FROM Products WHERE CategoryID =" & categoryID
        Dim conn As New SqlConnection("Data Source=MyPC;Initial Catalog=C:\SQL SERVER 2000 SAMPLE DATABASES\NORTHWND.MDF;Integrated Security=True")
        conn.Open()
        Dim cmd As New SqlCommand(sql, conn)
        Dim da As New SqlDataAdapter(cmd)
        Dim dt As New DataTable()
        da.Fill(dt)
        conn.Close()
        Return dt
    End Function

    Write a function Categories() to get Category data from database. Then bind this data to DropDownList. I have also mentioned DataValueField and DataTextField. Now write another function Products() to get products data as per selected category in DropdownList. At the end bind GridView control in SelectedIndexChange() method of DropDownList. In SelectedIndexChange() method, first I have get the CategoryID of Selected Item and provide this ID as parameter in Products() method to get data for this category.
     
  5. View the Web Site in browser and see the result. Select Category from DropDownList and see the data populated in GridView.

 

   
Add Article Comment:
Name :
Email Address :
   
Comments :
 
   
<< LINQ to SQL in ASP.NET

Disclaimer - Privacy
© 2002-2017 DevASP.net