积极答复者
UpdatePanel 使用出错

问题
-
hi,我使用UpdatePanel做关联更新,其UpdatePanel内的事件本来在PostBack是不会触发的(只应该在ScriptManager1.IsInAsyncPostBack触发),
但目前我的代码是UpdatePanel内的事件本来在PostBack后会触发,这在逻辑上就错了。请帮忙分析原因。具体代码如下:
当省的选择变化时候,市的确跟着变化(ScriptManager1.IsInAsyncPostBack),但此时点击页面UpdatePanel区外的一个Buton时候(PostBack),
事件“DropDownListAdminArea1_SelectedIndexChanged”还会触发。<asp:UpdatePanel ID="UpdatePanel1" runat="server" >
<ContentTemplate>
<table >
<tr>
<td>
<asp:Label ID="Label1" runat="server" Text="省/区/直辖市"></asp:Label>
</td>
<td>
<asp:DropDownList ID="DropDownListAdminArea1" runat="server"
AutoPostBack="True"
onselectedindexchanged="DropDownListAdminArea1_SelectedIndexChanged">
</asp:DropDownList>
</td>
<td>
</td>
<td>
<asp:Label ID="Label2" runat="server" Text="市"></asp:Label>
</td>
<td>
<asp:DropDownList ID="DropDownListAdminArea2" runat="server">
</asp:DropDownList>
</td>
</tr>
</table>
</ContentTemplate>
</asp:UpdatePanel>
答案
-
請問你在CodeBehind檔有寫什麼跟DropDownListAdminArea1有關的程式碼嗎?
以上說明若有錯誤請指教,謝謝。
http://www.dotblogs.com.tw/terrychuang/- 已标记为答案 he.d.d.shan 2012年12月6日 13:25
-
你好,
可以看一下在Page_Load event中是否有相关的绑定。
一般可以先判断是否是PostBack,再看是不是Async的。
- 已标记为答案 he.d.d.shan 2012年12月6日 13:25
全部回复
-
請問你在CodeBehind檔有寫什麼跟DropDownListAdminArea1有關的程式碼嗎?
以上說明若有錯誤請指教,謝謝。
http://www.dotblogs.com.tw/terrychuang/- 已标记为答案 he.d.d.shan 2012年12月6日 13:25
-
有,
protected void DropDownListAdminArea1_SelectedIndexChanged(object sender, EventArgs e)
{
string adminArea1 = DropDownListAdminArea1.SelectedValue;
if (string.IsNullOrEmpty(adminArea1))
{
DropDownListAdminArea2.Items.Clear();
this.DropDownListAdminArea2.Items.Insert(0, new ListItem("", "-请选择-"));
return;
}
else
{
System.Data.DataTable dt = GetAdminArea2(adminArea1);
this.ThisPage.DdlCodeNameDataBind(this.DropDownListAdminArea2, dt);
this.DropDownListAdminArea2.Items.Insert(0, new ListItem("", "-请选择-"));
}}
-
你好,
可以看一下在Page_Load event中是否有相关的绑定。
一般可以先判断是否是PostBack,再看是不是Async的。
- 已标记为答案 he.d.d.shan 2012年12月6日 13:25
-
非常感谢各位。
我找到原因了,但是怎么解决此问题还不知道。
我在“ protected override void SavePageStateToPersistenceMedium(object state)”和“ protected override object LoadPageStateFromPersistenceMedium()”方法中使用了自定义的保存读取ViewState,导致Async的Post后改变了ViewState,而然后正常Post过来又使用了过期的ViewState。好像说的有点绕,把代码贴出来:
private const string ViewState_HiddenInput_Key = @"__ViewStateKey"; protected override void SavePageStateToPersistenceMedium(object state) { if (state == null) { base.SavePageStateToPersistenceMedium(state); } else { string key; key = "$" + Guid.NewGuid().ToString() + "$"; MySaveViewState(key, state);//保存到服务器硬盘或内存 ClientScript.RegisterHiddenField(ViewState_HiddenInput_Key, key); } } protected override object LoadPageStateFromPersistenceMedium() { string key = Request.Form[ViewState_HiddenInput_Key]; if (string.IsNullOrEmpty(key)) { return base.LoadPageStateFromPersistenceMedium(); } else if (key.Length == ViewStateKeyLen && key[0] == '$') { object state = MyGetViewState(key);//从服务器硬盘或内存读取 if (state != null) { return state; } else { return null; } } else { return null; } }