Dropdownlist(ddlState) 值丢失时,在更新面板中的 AsyncPostBackTrigger 之后在
本文关键字:更新 之后 AsyncPostBackTrigger ddlState Dropdownlist | 更新日期: 2023-09-27 18:34:31
我有ddlCountry下拉列表和ddlState下拉列表。 ddlState 位于更新面板中。在选择国家时,我使用 ddlCountry 的选定索引更改事件填充了其州。我在绑定后将起始值"-1"添加到两个下拉列表中,如果下拉列表内容是 [选择] 值"-1",我会在 Javascript 中检查,然后发出警报并返回 false。
但是验证适用于 ddlCountry,对于 ddlState,在 ajax 更新面板触发和验证失败后,其值变为"空白"。
我应该如何解决这些问题?我不知道为什么在AsyncPostBackTrigger之后ddlState值丢失。
//javascript validation
function validateForm()
{
if (ddlCountry .value == "-1")
{
alert("Country should not be blank.");
ddlCountry .focus();
return false;
}
if (ddlState .value == "-1")
{
alert("State should not be blank.");
ddlState .focus();
return false;
}
return true;
}
//Aspx code
<asp:DropDownList ID="ddlCountry " runat="server" CssClass="csstextbox" Width="207px"
AutoPostBack="true" OnSelectedIndexChanged="ddlCountry _SelectedIndexChanged">
</asp:DropDownList>
<asp:UpdatePanel ID="updatePanelState" runat="server">
<ContentTemplate>
<asp:DropDownList ID="ddlState " runat="server" CssClass="csstextbox" Width="177px">
</asp:DropDownList>
</ContentTemplate>
<Triggers>
<asp:AsyncPostBackTrigger ControlID="ddlCountry" EventName="SelectedIndexChanged" />
</Triggers>
</asp:UpdatePanel>
//Code Behind
protected void Page_Load(object sender, EventArgs e)
{
if(!IsPostBack)
{
BindCountry();
}
}
protected void ddlCountry_SelectedIndexChanged(object sender, EventArgs e)
{
int countryID = Convert.ToInt32(ddlCountry.SelectedItem.Value);
ifcountryID == -1)
{
return;
}
strSQL = @"SELECT State_ID,State_Desc
FROM State_Master
WHERE countryID = '" + countryID + @"';
DataTable dataTableState = null;
dataTableState = objSqlDbComm.ExecuteDatasetQuery(strSQL).Tables[0];
var dictioneryState = new Dictionary<int, string>();
foreach(DataRow dr in dataTableStudy.Rows)
{
dictioneryState .Add(Convert.ToInt32(dr["State_ID"]), dr["State_Desc"].ToString());
}
ddlState.DataTextField = "Value";
ddlState.DataValueField = "Key";
ddlState.DataSource = dictioneryState;
ddlState.DataBind();
ddlState.Items.Insert(0, new ListItem("[Select]", "-1"));
ddlState.Items[0].Selected = true;
}
为两个下拉列表启用视图状态为 true,如下所示:
<asp:DropDownList ID="ddlState " runat="server" CssClass="csstextbox" Width="177px" EnableViewState=true> </asp:DropDownList>
它可能会解决您的问题。
谢谢