我有两个DDL的表单,名为
州和城市
状态:
<asp:UpdatePanel ID="States" runat="server" UpdateMode="Conditional">
<Triggers>
<asp:AsyncPostBackTrigger ControlID="States"EventName="SelectedIndexChanged" />
</Triggers>
<ContentTemplate>
<asp:DropDownList ID="States" runat="server"
AutoPostBack="True" DataSourceID="StatesObjectDataSource"
AppendDataBoundItems="true"
onselectedindexchanged="States_SelectedIndexChanged">
<asp:ListItem Value="-1" Text="- None -"/>
</asp:DropDownList>
<asp:ObjectDataSource ID="StatesObjectDataSource" runat="server"
onselecting="StatesObjectDataSource_Selecting"
SelectMethod="GetStates"
TypeName="Something">
</asp:ObjectDataSource>
</ContentTemplate>
</asp:UpdatePanel>城市:
<asp:DropDownList ID="Cities" runat="server">
</asp:DropDownList>当他们选择一个州时,我想用该州的所有城市填充Cities DDL。
在后面的代码中,我能够获得
States_SelectedIndexChanged(object sender, EventArgs e)我试着用这个来填充城市DDL
Cities.Items.Add(new ListItem(city,city));但是,我没有看到我的城市DDL被填充
发布于 2008-11-18 22:52:21
我建议在ViewState中创建一个私有属性来保存物理对象的集合。然后将对象添加到该列表中,然后将对象列表数据绑定到下拉列表中。
页后
<asp:DropDownList runat="server" ID="ddlCity" DataValueField="Key" DataTextField="Value">
</asp:DropDownList>代码隐藏
private List<KeyValuePair<string, string>> ListData
{
get { return (List<KeyValuePair<string, string>>) (ViewState["ListData"] ??
(ViewState["ListData"] = new List<KeyValuePair<string, string>>())); }
set { ViewState["ListData"] = value; }
}
protected void States_SelectedIndexChanged_SelectedIndexChanged(object sender, EventArgs e)
{
ListData.Add(new KeyValuePair<string, string>(ddlCitys.SelectedValue, ddlCitys.SelectedValue));
ddlCitys.DataSource = ListData;
ddlCitys.DataBind();
}get语句还在ListData属性上使用延迟加载,因此在访问列表时不会遇到空引用异常。
发布于 2008-11-18 23:01:01
如果可能的话,我建议使用CascadingDropDown Extender而不是UpdatePanel。重新发明这个轮子是没有用的,而且Toolkit控件使用web服务而不是部分回发(快得多)。
发布于 2008-11-18 23:05:43
将您的城市DropDownList放在更新面板中。
https://stackoverflow.com/questions/299969
复制相似问题