Asp.net 下拉列表和更新面板
我开发了地址控制,它包含2个下拉列表(用于城市和国家)和几个文本框。第二个DropDownList数据源依赖于第一个DropDownList数据源Asp.net 下拉列表和更新面板,asp.net,ajax,webforms,drop-down-menu,Asp.net,Ajax,Webforms,Drop Down Menu,我开发了地址控制,它包含2个下拉列表(用于城市和国家)和几个文本框。第二个DropDownList数据源依赖于第一个DropDownList数据源 <fieldset> <legend><%=Title%></legend> <asp:UpdatePanel ID="UpdatePanel1" runat="server"> <ContentTemplate> <div>
<fieldset>
<legend><%=Title%></legend>
<asp:UpdatePanel ID="UpdatePanel1" runat="server">
<ContentTemplate>
<div>
<label for="<%=ddlCountry.ClientID %>">Country</label>
<asp:DropDownList runat="server" ID="ddlCountry"
DataTextField="Name" DataValueField="Id"
DataSource="<%#Facade.Addresses.GetCountries() %>"
AutoPostBack="true"
OnSelectedIndexChanged="ddlCountry_SelectedIndexChanged"
/>
</div>
<div>
<label for="<%=ddlCity.ClientID %>">City</label>
<asp:DropDownList runat="server" ID="ddlCity"
DataTextField="Name" DataValueField="Name" />
</div>
</ContentTemplate>
<Triggers>
<asp:AsyncPostBackTrigger ControlID="ddlCountry" EventName="SelectedIndexChanged" />
</Triggers>
</asp:UpdatePanel>
<div>
<label for="<%=txtStreet.ClientID %>">Street</label>
<uc:TextBox ID="txtStreet" Text="<%#Address.Street %>" runat="server" />
</div>
<div>
<label for="<%=txtBlock.ClientID %>">Block</label>
<uc:TextBox ID="txtBlock" Text="<%#Address.Block %>" runat="server" />
</div>
<div>
</fieldset>
它工作得很好。但如果页面上的其他控件导致回发,则当ddlCity中的SelectedValue设置为第一个(默认)值时
如何避免它?将
页面上的代码移动到页面加载
并将其放入中!iPostBack
protected void Page_Load(object sender, EventArgs e)
{
if (!IsPostBack)
{
ddlCountry.DataBind();
ddlCity.DataSource = Facade.Addresses.GetCities(countryId);
ddlCity.DataBind();
}
}
放置ddlCountry.DataBind();内部if条件为什么要检查两次“IsPostBack”?Fahad有解决方案。naveen的建议只是为了符合标准,尽管您只需要打开!IsPostback
protected void Page_Load(object sender, EventArgs e)
{
if (!IsPostBack)
{
ddlCountry.DataBind();
ddlCity.DataSource = Facade.Addresses.GetCities(countryId);
ddlCity.DataBind();
}
}