当我动态地将自定义用户控件添加到面板时,用户控件将丢失所有事件处理
我有一个 aspx 页面,它根据一组单选按钮的输入动态地将 UserControl 加载到 Panel 对象中。 UserControl 在回发时成功添加并正确显示到面板,并在 UC 中调用 Page_Load() 事件,但当我以任何会触发事件的方式与表单交互时,回发时不会捕获该事件。
我尝试在 Page_Load() 中添加事件处理关联(我知道它会被调用),并在 ASP.NET 标记中添加关联,结果没有任何差异。
这就是我添加控件的方式(对象名称已被简化):
private UserControl _control;
protected void RadioButtonGroup_CheckedChanged(object sender, EventArgs e)
{
RadioButton radioButton = (RadioButton)sender;
if (radioButton == RadioButton1Ctl)
{
_control = (UserControl1)LoadControl("~/Controls/UserControl1.ascx");
PanelCtl.Controls.Add(_control);
}
else if (radioButton == RadioButton2Ctl)
{
_control = (UserControl2)LoadControl("~/Controls/UserControl2.ascx");
PanelCtl.Controls.Add(_control);
}
}
正如我所说,当我单击任何按钮或有任何应绑定在 UC 上的回发事件时,控件就会从控件中删除,从而成功添加控件。页面和事件不会被触发。
任何帮助将不胜感激。
I've got aspx page which dynamically loads UserControl into a Panel object based on the input on a set of radio buttons. The UserControl successfully adds and displays properly to the Panel on postback and calls the Page_Load() event just fine in the UC but when I interact with the form in any way that would trigger an event, the event is not captured on the postback.
I've tried to add the event handling association in the Page_Load() which I know gets called as well as adding the association in the ASP.NET tag without any difference in result.
This is how I am adding the control (object names have been simplified):
private UserControl _control;
protected void RadioButtonGroup_CheckedChanged(object sender, EventArgs e)
{
RadioButton radioButton = (RadioButton)sender;
if (radioButton == RadioButton1Ctl)
{
_control = (UserControl1)LoadControl("~/Controls/UserControl1.ascx");
PanelCtl.Controls.Add(_control);
}
else if (radioButton == RadioButton2Ctl)
{
_control = (UserControl2)LoadControl("~/Controls/UserControl2.ascx");
PanelCtl.Controls.Add(_control);
}
}
As I said, the control gets successfully added by when I click any buttons or have any postback events which should be bound on the UC, the control gets removed from the page and events aren't fired.
Any help would be greatly appreciated.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
发生这种情况是因为控件是动态添加的。我建议使用
DynamicControlsPlaceHolder
而不是面板。当页面回发时,它将为您保留您的控件。动态控件占位符:
http://www.denisbauer.com/ASPNETControls/DynamicControlsPlaceholder.aspx
另一种选择是在重新加载 ViewState 之前,在每次回发时重新创建控件。我建议使用
OnInit
。DynamicControlsPlaceHolder 消除了所有艰苦的工作,因此这可能是您的最佳选择。
This is happening because the controls are being added dynamically. I would suggest using the
DynamicControlsPlaceHolder
instead of a Panel. It will persist your controls for you when the page is posted back.DynamicControlsPlaceHolder:
http://www.denisbauer.com/ASPNETControls/DynamicControlsPlaceholder.aspx
The other alternative is to recreate the controls at every postback, before the ViewState is reloaded. I would suggest using
OnInit
.The DynamicControlsPlaceHolder takes all of the hard work away, so that might be the best option for you.