gpt4 book ai didi

asp.net - 下拉列表和更新面板

转载 作者:行者123 更新时间:2023-12-04 06:19:34 25 4
gpt4 key购买 nike

我开发了地址控件,其中包含 2 个 DropDownList(用于城市和国家/地区)和几个 TextBox。第二个 DropDownList 数据源依赖于第一个 DropDownList 数据源。

<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>

背后的代码
protected void Page_Init(object sender, EventArgs e)
{

ddlCountry.DataBind();
if (!IsPostBack)
{
ddlCity.DataSource = Facade.Addresses.GetCities(countryId);
ddlCity.DataBind();
}

}

protected void ddlCountry_SelectedIndexChanged(object sender, EventArgs e)
{
ddlCity.DataSource = Facade.Addresses.GetCities(countryId);
ddlCity.DataBind();
}

它运作良好。但是如果页面上的其他控件导致回发,当 ddlCity 中的 SelectedValue 设置为第一个(默认)值时。

我该如何避免?

最佳答案

将代码移至 Page_InitPage_Load放在里面!IsPostBack

protected void Page_Load(object sender, EventArgs e)
{
if (!IsPostBack)
{
ddlCountry.DataBind();
ddlCity.DataSource = Facade.Addresses.GetCities(countryId);
ddlCity.DataBind();
}
}

关于asp.net - 下拉列表和更新面板,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/6772403/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com