简体   繁体   English

使用checkedChanged EventHandler将PostBackTrigger和AsyncPostBackTriggers添加到UpdatePanel以获取动态生成的复选框

[英]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>中添加动态创建的ControlsID

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 the ScriptManager control. 若要以编程方式注册回发控件,请使用ScriptManager控件的RegisterAsyncPostBackControl方法。 Then call the Update method of the UpdatePanel 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.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM