动态加载控件的视图状态问题
我的动态加载控件的事件有问题。
以下是我加载控件的方式:
protected void Page_Load(object sender, EventArgs e)
{
LoadSubPageEditTemplate();
}
protected void LoadSubPageEditTemplate()
{
pnlPageTemplate.Controls.Clear();
BaseOfferAdmin adminControl = (BaseOfferAdmin)this.LoadControl("~/Controls/SingleOfferAdmin.ascx");
if (adminControl != null)
{
adminControl.ID = "Control_ID"
pnlPageTemplate.Controls.Add(adminControl);
}
}
}
LoadSubPageEditTemplate() 也是从页面上的按钮事件触发的,因为我必须在按钮事件执行后更新它。
该控件加载完美,我已经使用基本按钮对其进行了测试,并且回发按预期触发。然而,我用复选框得到了奇怪的结果。
<asp:CheckBox runat="server" ID="cbOptionalAction" Text="CheckBox" AutoPostBack="true" OnCheckedChanged="cbOptionalAction_CheckChanged" />
CheckedChanged 事件仅在选中复选框时触发,我猜这意味着我的控件视图状态存在一些问题。如果页面上有多个复选框,并且其中一个创建回发,则为每个选中的复选框触发 checkchanged 事件。
感谢任何人抽出时间来帮助我!
伊格努斯
I am having issues with my dynamically loaded control's events.
Here is how I am loading the control:
protected void Page_Load(object sender, EventArgs e)
{
LoadSubPageEditTemplate();
}
protected void LoadSubPageEditTemplate()
{
pnlPageTemplate.Controls.Clear();
BaseOfferAdmin adminControl = (BaseOfferAdmin)this.LoadControl("~/Controls/SingleOfferAdmin.ascx");
if (adminControl != null)
{
adminControl.ID = "Control_ID"
pnlPageTemplate.Controls.Add(adminControl);
}
}
}
The LoadSubPageEditTemplate() is also fired from a button event on the page, since I have to update it after the button event has executed.
The control loads perfectly, I have tested it with a basic button and the postback fires as expected. However, I am getting weird results with CheckBoxes.
<asp:CheckBox runat="server" ID="cbOptionalAction" Text="CheckBox" AutoPostBack="true" OnCheckedChanged="cbOptionalAction_CheckChanged" />
The CheckedChanged event only fires when the Checkbox is checked, which I guess means there is some issue with my control viewstate. If I have multiple checkboxes on the page, and one creates a postback, the checkchanged event is fired for each checkbox that is checked.
Thanks for anyone taking some time out to help me out!
Ignus
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
请仅在页面未回发时加载控件。
Please load control only when page is not postback.