[英]Dropdown list items based on value of another drop down list
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. 以下是html和C#代码。
<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 并为这样的第一个下拉列表的SelectedIndexChanged添加处理程序
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. 使用source初始化第二个下拉列表,具体取决于第一个下拉列表的当前选定值。
So, when user changing selected value of first drop down - postback occurs and second drop down filling with required values 因此,当用户更改第一个下拉列表的选定值时-发生回发,第二个下拉列表填充所需的值
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.