我有两个下拉列表,其中一个在页面加载时填充,另一个只应在第一个中选择一个值时填充。第一个下拉列表定义为:
<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
函数动态更新该下拉菜单中的其他下拉列表?
答案 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();