I am trying to create a second drop down list which based on the value of the first drop down list adjusts the list items as needed. I have written the following code, but even though there are no compiling errors the second drop down list remains always empty. Below are html and C# code.
<table> <tr><td>System</td> <td> <asp:DropDownList ID="systemddl" runat="server" AutoPostBack="True" Height="24px" Width="142px"> <asp:ListItem>G1</asp:ListItem> <asp:ListItem>D2</asp:ListItem> <asp:ListItem> D3</asp:Listitem> <asp:ListItem> M4</asp:Listitem> <asp:ListItem> I5</asp:Listitem> </asp:DropDownList></td> </tr> <tr> <td>KPI</td> <td> <asp:DropDownList ID="kpiddl" runat="server" AutoPostBack="True" Height="24px" Width="142px"></asp:DropDownList></td> </tr> </table>
protected void systemddl_SelectedIndexChanged(object sender, EventArgs e)
{
if (systemddl.SelectedValue == "G1")
{
var items1 = new List<ListItem>()
{
new ListItem("TEST1"),
new ListItem("")
};
kpiddl.DataSource = items1;
kpiddl.DataBind();
kpiddl.SelectedValue = "";
if (systemddl.SelectedValue == "D2")
{
var items2 = new List<ListItem>()
{
new ListItem("1"),
new ListItem("2"),
new ListItem("3"),
new ListItem("4")
};
kpiddl.DataSource = items2;
kpiddl.DataBind();
}
if (systemddl.SelectedValue == "I5")
{
var items3 = new List<ListItem>()
{
new ListItem("Total"),
new ListItem("V Completed"),
new ListItem("R found"),
new ListItem("R sold"),
new ListItem("A found"),
new ListItem("Asold"),
new ListItem("")
};
kpiddl.DataSource = items3;
kpiddl.DataBind();
kpiddl.SelectedValue = "";
}
if (systemddl.SelectedValue == "D3")
{
var items4 = new List<ListItem>()
{
new ListItem("FRFT"),
new ListItem("")
};
kpiddl.DataSource = items4;
kpiddl.DataBind();
}
if (systemddl.SelectedValue == "M4")
{
var items5 = new List<ListItem>()
{
new ListItem("A"),
new ListItem("B"),
new ListItem("C"),
new ListItem("D"),
new ListItem("")
};
kpiddl.DataSource = items5;
kpiddl.DataBind();
kpiddl.SelectedValue = "";
}
Change markup like this
<asp:DropDownList
ID="ddl_1"
runat="server"
AutoPostBack="true"
OnSelectedIndexChanged="ddl_1_SelectedIndexChanged">
</asp:DropDownList>
<asp:DropDownList
ID="ddl_2"
runat="server">
and add handler for SelectedIndexChanged of first drop down like this
protected void ddl_1_SelectedIndexChanged(object sender, EventArgs e)
{
switch (this.ddl_1.SelectedValue)
{
case "A":
this.ddl_2.DataSource = source1;
break;
case "B":
this.ddl_2.DataSource = source2;
break;
default:
break;
}
this.ddl_2.DataBind();
}
Init second dropdown with source, which depends of current selected value of first drop down.
So, when user changing selected value of first drop down - postback occurs and second drop down filling with required values
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.