Home  »  Code Snippets  »  ASP.Net  »

fill one dropdownlist based on selection of another dropdownlist

 Posted By : Manoranjan SahooPosted Date : 26/11/2010 06:22:16 AM Category : ASP.Net Points : 5

here we have to fill one dropdownlist based on the selection of items from another dropdownlist.

for ex : suppose we have two dropdownlist named as DropDownList1 and DropDownList2

Now we have to fill DropDownList2 on the selection of DropDownList1.

To achieve this we have to set AutoPostBack property of DropDownList1 to true and set the code to fill DropDownList2 in side onselectedindexchanged event.

see below code :

aspx code :

<%@ Page Language="C#" AutoEventWireup="true" CodeFile="Default.aspx.cs" Inherits="Default" %>

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">

<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
    <title>DropDownList Example</title>
      <script type="text/javascript">
   
      function pageLoad() {
      }
   
    </script>
</head>
<body>
    <form id="form1" runat="server">
    <div>
        <asp:ScriptManager ID="ScriptManager1" runat="server" />
          <asp:DropDownList ID="DropDownList1" runat="server" onselectedindexchanged="DropDownList1_SelectedIndexChanged" AutoPostBack ="true" >
        </asp:DropDownList>
        <asp:DropDownList ID="DropDownList2" runat="server">
        </asp:DropDownList>
    </div>
    </form>
</body>
</html>

in aspx.cs page :

protected void Page_Load(object sender, EventArgs e)
{
     if (!IsPostBack)
     {
         fillDropDownList1();
     }
}
void fillDropDownList1()
{
        DropDownList1.Items.Clear();
        DropDownList1.Items.Add(new ListItem("AAA", "1"));
        DropDownList1.Items.Add(new ListItem("BBB", "2"));
        DropDownList1.Items.Add(new ListItem("CCC", "3"));
        DropDownList1.Items.Add(new ListItem("DDD", "4"));
}
void fillDropDownList2()
{
        DropDownList2.Items.Clear();
        if (DropDownList1.SelectedIndex == 0)
        {
            DropDownList2.Items.Add(new ListItem("AAA1", "1"));
            DropDownList2.Items.Add(new ListItem("AAA2", "2"));
        }
        else if (DropDownList1.SelectedIndex == 1)
        {
            DropDownList2.Items.Add(new ListItem("BBB1", "1"));
            DropDownList2.Items.Add(new ListItem("BBB2", "2"));
        }
        else if (DropDownList1.SelectedIndex == 2)
        {
            DropDownList2.Items.Add(new ListItem("CCC1", "1"));
            DropDownList2.Items.Add(new ListItem("CCC2", "2"));
        }
        else if (DropDownList1.SelectedIndex == 3)
        {
            DropDownList2.Items.Add(new ListItem("DDD1", "1"));
            DropDownList2.Items.Add(new ListItem("DDD2", "2"));
        }
}
protected void DropDownList1_SelectedIndexChanged(object sender, EventArgs e)
{
     fillDropDownList2();
}

Thank You.

Responses
No responses found for this thread. Be the first to respond this thread.
Post Reply
Login to post Response.