当我动态地将自定义用户控件添加到面板时,用户控件将丢失所有事件处理

发布于 2024-12-05 04:29:29 字数 935 浏览 1 评论 0原文

我有一个 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

乜一 2024-12-12 04:29:29

发生这种情况是因为控件是动态添加的。我建议使用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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文