更清晰的解释这里是我在 .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>
在我的
.cs
代码,我像这样动态地为 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);
现在我的问题是当我更改复选框时,整个页面必须加载而不是
UpdatePanel
的内容.请注意,动态创建的复选框的 EventHandler 正在触发,但不在 UpdatePanel 内。如何添加
ID
的动态创建的 Controls
在 <Triggers>
的UpdatePanel
?
最佳答案
无法将动态(或以编程方式创建的)控件添加到标记中,因此,您必须使用 ScriptManager
注册控件。创建之后。
每 AsyncPostBackTrigger
documentation ,添加一个 AsyncPostBackTrigger
不支持以编程方式控制:
To programmatically register a postback control, use the
RegisterAsyncPostBackControl
method of theScriptManager
control. Then call the Update method of theUpdatePanel
control when the control posts back.
基本上你应该做的是在创建 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);
重要提示:您的内部
chkID_OnRow_Check
回调/事件处理函数,确保你调用 UpdatePanel1.Update()
.更新 2013-02-20
由于我对您收到的异常的理解,请考虑使 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"));
关于c# - 将 PostBackTrigger 和 AsyncPostBackTriggers 添加到 UpdatePanel 以使用 checkChanged EventHandler 动态生成复选框,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/14742426/