Monday, February 1, 2010

Nesting 3 DropdownLists in ASP.net C#

  •   Add 3 DropdownList controls to .aspx page.
  • Enable AutoPostback for the 1st and 2nd Dropdownlists.
public partial class Default : System.Web.UI.Page
{
    protected string[] d1 = { "select","eng", "mba"};

    protected string[] eng = { "select","ec","cs" };
    protected string []mba = { "select","fin", "hr" };
   

    protected string []ec = { "ec1", "ec2"};
    protected string []cs = { "cs1", "cs2"};

   
    protected string []fin = { "fin1", "fin2"};
    protected string []hr = { "hr1", "hr2"};

  
    protected void Page_Load(object sender, EventArgs e)
    {
        if (!IsPostBack)
        {
            DropDownList1.DataSource = d1;
            DropDownList1.DataBind();
        }
       
    }
    protected void DropDownList1_SelectedIndexChanged(object sender, EventArgs e)
    {

        if (DropDownList1.SelectedIndex == 1)
        {
            DropDownList2.DataSource = eng;
            DropDownList2.DataBind();
        }
        else if (DropDownList1.SelectedIndex == 2)
        {
            DropDownList2.DataSource = mba;
            DropDownList2.DataBind();
        }
       
    }
    protected void DropDownList2_SelectedIndexChanged(object sender, EventArgs e)
    {
      
        switch (DropDownList2.SelectedIndex)
        {
            case 1:
                {

                    if (DropDownList1.SelectedIndex == 1)
                    {
                        DropDownList3.DataSource = ec;
                        DropDownList3.DataBind();
                       
                    }
                    else if (DropDownList1.SelectedIndex == 2)
                    {
                        DropDownList3.DataSource = fin;
                        DropDownList3.DataBind();
                       
                    }
                    break;
                }
            case 2:
                {
                    if (DropDownList1.SelectedIndex == 1)
                    {
                        DropDownList3.DataSource = cs;
                        DropDownList3.DataBind();
                       
                    }
                    else if (DropDownList1.SelectedIndex == 2)
                    {
                        DropDownList3.DataSource = hr;
                        DropDownList3.DataBind();
                       
                    }
                }
                break;
         }

    }
}

No comments:

Post a Comment