[英]Adding PostBackTrigger and AsyncPostBackTriggers to UpdatePanel for dynamically generated checkboxes with checkedChanged EventHandler
For more clear explanation here's my code on .asp
file: 有关更明确的解释,请
.asp
文件中的代码:
<asp:UpdatePanel ID="updPnlTabs" runat="server" >
<Triggers>
<asp:PostBackTrigger ControlID="btnSave" />
</Triggers>
<ContentTemplate>
<asp:Panel ID="pnlCheckList" runat="server" style="margin-bottom: 10px;" CssClass="listingDummyTab">
</asp:Panel>
</ContentTemplate>
</asp:UpdatePanel>
On my .cs
code, I dynamically created checkeboxes to the pnlCheckList like this: 在我的
.cs
代码中,我动态创建了checkebox到pnlCheckList,如下所示:
CheckBox chkModuleID = new CheckBox();
chkModuleID.ID = drNew[def.ID].ToString();
chkModuleID.AutoPostBack = true;
chkModuleID.CheckedChanged += new EventHandler(chkID_OnRow_Check);
pnlCheckList.Controls.Add(chkModuleID);
Now my problem here is when I change the check boxes the whole page have to load instead of the content of the UpdatePanel
. 现在我的问题是当我更改复选框时,必须加载整个页面而不是
UpdatePanel
的内容。 Note that the EventHandler for the dynamically created checkboxes is firing but not inside the UpdatePanel. 请注意,动态创建的复选框的EventHandler正在触发,但不在UpdatePanel内。
How can I add the ID
's of the dynamically created Controls
in <Triggers>
of the UpdatePanel
? 如何在
UpdatePanel
<Triggers>
中添加动态创建的Controls
的ID
?
There is no way of adding dynamic (or programmatically created) controls to the markup, therefore, you must register the control with the ScriptManager
after creating it. 无法向标记添加动态(或编程创建)控件,因此,必须在创建
ScriptManager
后使用ScriptManager
注册该控件。
Per the AsyncPostBackTrigger
documentation , adding an AsyncPostBackTrigger
control programmatically is not supported: 根据
AsyncPostBackTrigger
文档 ,不支持以编程方式添加AsyncPostBackTrigger
控件:
To programmatically register a postback control, use the
RegisterAsyncPostBackControl
method of theScriptManager
control.若要以编程方式注册回发控件,请使用
ScriptManager
控件的RegisterAsyncPostBackControl
方法。 Then call the Update method of theUpdatePanel
control when the control posts back.然后在控件回发时调用
UpdatePanel
控件的Update方法。
Basically what you should be doing is registering the CheckBox control after creating it. 基本上你应该做的是在创建CheckBox控件之后注册它。
// original code
CheckBox chkModuleID = new CheckBox();
chkModuleID.ID = drNew[def.ID].ToString();
chkModuleID.AutoPostBack = true;
chkModuleID.CheckedChanged += new EventHandler(chkID_OnRow_Check);
pnlCheckList.Controls.Add(chkModuleID);
// register the control to cause asynchronous postbacks
ScriptManager.GetCurrent(this).RegisterAsyncPostBackControl(chkModuleID);
Important: Inside of your chkID_OnRow_Check
callback/eventhandler function, ensure you call UpdatePanel1.Update()
. 重要提示:在
chkID_OnRow_Check
回调/事件处理程序函数内部,确保调用UpdatePanel1.Update()
。
Update 2013-02-20 更新2013-02-20
Due to the my understanding of the exception you are receiving, consider making the CheckBox IDs unique. 由于我对您收到的异常的理解,请考虑使CheckBox ID唯一。
// One possibility is this - assuming you don't require a consistent ID
chkModuleID.ID = String.Format("{0}-{1}", drNew[def.ID].ToString(), Guid.NewGuid().ToString("N"));
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.