根据另一个下拉列表中的选定值动态填充下拉列表

时间:2013-03-18 17:53:21

标签: c# asp.net

我有两个下拉列表,其中一个在页面加载时填充,另一个只应在第一个中选择一个值时填充。第一个下拉列表定义为:

<asp:DropDownList ID = "base_ddl" runat="server" ToolTip="Base" OnSelectedIndexChanged = "BaseSelected">
//  snip...
</asp:DropDownList>

然后,第二个下拉列表(secondary_ddl)将填入OnSelectedIndexChanged方法BaseSelected

    public void BaseSelected(object sender, EventArgs e)
    {
        if (base_ddl.SelectedValue == "-1")
        {
            secondary_ddl.Visible = false;
        }
        else 
        {
            secondary_ddl.Items.Clear();

            // extract information to populate dropdown here
            // snip.
            IList<InfoContainer> info = getInfoBasedOnSelected(base_ddl.Value);

            foreach (InfoContainer i in info)
            {
                secondary_ddl.Items.Add(new ListItem(i.name, i.value));
            }
        }
    }

这不起作用,导致这个问题:为什么?如何从OnSelectedIndexChanged函数动态更新该下拉菜单中的其他下拉列表?

1 个答案:

答案 0 :(得分:1)

AutoPostBack="True"

的第一组base_ddl属性

然后

secondary_ddl.Items.Clear();
IList<InfoContainer> info = getInfoBasedOnSelected(base_ddl.Value);
secondary_ddl.DataTextField = "name";
secondary_ddl.DataValueField = "value";
secondary_ddl.DataSource = info;
secondary_ddl.DataBind();