For example let's take two DropdownLists where the first acts as a parent to the second.
Let's assume to list Movie names in the first DropdownList and to list in the second DropdownList the Theatres showing the selected Movie.
- Add two DropdownList controls to the .aspx page and enable AutoPostback in the properties window of first DropdownList.
- Add the fallowing code:
public partial class Default : System.Web.UI.Page
{
// First DropdownList items
protected string[] Movies ={ "select a movie", "Movie_1", "Movie_2", "Movie_3" };
// Second DropdownList items. One among these to be choosen.
protected string[] Movie_1={"Theatre_1","Theatre_2","Theatre_3"};
protected string[] Movie_2 ={ "Theatre_4", "Theatre_5" };
protected string[] Movie_3 ={ "Theatre_5", "Theatre_6", "Theatre_7" };
protected void Page_Load(object sender, EventArgs e)
{
if (!IsPostBack)
{
DropDownList1.DataSource = Movies;
DropDownList1.DataBind();
}
}
protected void DropDownList1_SelectedIndexChanged(object sender, EventArgs e)
{
if (DropDownList1.SelectedIndex == 1)
{
DropDownList2.DataSource = Movie_1;
DropDownList2.DataBind();
}
if (DropDownList1.SelectedIndex == 2)
{
DropDownList2.DataSource = Movie_2;
DropDownList2.DataBind();
}
if (DropDownList1.SelectedIndex == 3)
{
DropDownList2.DataSource = Movie_3;
DropDownList2.DataBind();
}
}
}
{
// First DropdownList items
protected string[] Movies ={ "select a movie", "Movie_1", "Movie_2", "Movie_3" };
// Second DropdownList items. One among these to be choosen.
protected string[] Movie_1={"Theatre_1","Theatre_2","Theatre_3"};
protected string[] Movie_2 ={ "Theatre_4", "Theatre_5" };
protected string[] Movie_3 ={ "Theatre_5", "Theatre_6", "Theatre_7" };
protected void Page_Load(object sender, EventArgs e)
{
if (!IsPostBack)
{
DropDownList1.DataSource = Movies;
DropDownList1.DataBind();
}
}
protected void DropDownList1_SelectedIndexChanged(object sender, EventArgs e)
{
if (DropDownList1.SelectedIndex == 1)
{
DropDownList2.DataSource = Movie_1;
DropDownList2.DataBind();
}
if (DropDownList1.SelectedIndex == 2)
{
DropDownList2.DataSource = Movie_2;
DropDownList2.DataBind();
}
if (DropDownList1.SelectedIndex == 3)
{
DropDownList2.DataSource = Movie_3;
DropDownList2.DataBind();
}
}
}

No comments:
Post a Comment