下拉列表项基于另一个下拉列表的值

时间:2015-09-17 11:38:13

标签: c# asp.net

我正在尝试创建第二个下拉列表,该列表基于第一个下拉列表的值,根据需要调整列表项。我编写了以下代码,但即使没有编译错误,第二个下拉列表仍然是空的。下面是html和C#代码。



  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 = "";
        }
&#13;
&#13;
&#13;

 $(document).ready(function(){
  // ISSUE1  
  $('#selReg').change(function() {
            alert("REGIONE SELEZIONATA");
  });// ISSUE2  
 });// ISSUE3  

1 个答案:

答案 0 :(得分:1)

像这样更改标记

<asp:DropDownList 
    ID="ddl_1" 
    runat="server"
    AutoPostBack="true"
    OnSelectedIndexChanged="ddl_1_SelectedIndexChanged">
</asp:DropDownList>
<asp:DropDownList 
    ID="ddl_2" 
    runat="server">


并添加第一个下拉列表的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();
}

使用source初始化第二个下拉列表,它取决于当前所选的第一个下拉值。
因此,当用户更改第一次下拉的选定值 - 回发时,第二次下拉填充所需的值