简体   繁体   English

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

[英]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.

相关问题 根据另一个下拉列表在下拉列表中显示数据 - Display data in drop down based on another dropdown list 根据MVC 4中的另一个下拉列表选择值填充下拉列表 - Populate a drop down list based on another drop down list selected value in MVC 4 如何选择下拉列表值以在网格视图的另一个下拉列表中获取结果 - How to select DropDown List value to obtain results in another drop down list in Grid View 如何基于另一个下拉列表启用和禁用下拉列表项 - How to enable and disable dropdown list items based on another dropdown list 如果在项目列表中找不到所选值,则不绑定下拉列表 - Do not bind drop down list if selected value not found in list of items 根据从“选择”下拉列表中选择的值更新列表项上的字段 - Updating a field on list items based on the value selected from drop down “choice” column 根据所选的第一个下拉列表项加载第二个下拉列表项 - Load 2nd drop down list items based on 1st selected drop down list item 下拉列表项的工具提示 - Tooltip for Drop down list items 从数据库添加项目和下拉列表的值 - Add items and value of the drop down list from the database 基于下拉列表选择值的文本消息 - textbox message based on drop down list selected value
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM