C# ASP.NET 动态添加 UserControl 到 PlaceHolder,不触发 Click 事件,只触发 Page_Load

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/19301005/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-10 14:41:41  来源:igfitidea点击:

ASP.NET dynamically adding UserControl to PlaceHolder, not fire Click Event, only Page_Load

c#asp.netuser-controlsmaster-pagesplaceholder

提问by Pepa Zapletal

In my ASP.Net page I have PlaceHolder and Button. When user click on this button, I add some UserControls from my Interface method getControl to the PlaceHolder. Code:

在我的 ASP.Net 页面中,我有 PlaceHolder 和 Button。当用户单击此按钮时,我将一些 UserControl 从我的接口方法 getControl 添加到 PlaceHolder。代码:

    protected void ActionBtn_Click(object sender, EventArgs e)
    {
        if (provider != null)
        {
            actualObject = (PlaceHolder)provider.getControl();
            PlaceHolder1.Controls.Add(actualObject);
        }
    }

Method getControl:

方法getControl:

    public object getControl()
    {
        ph = new PlaceHolder();

        exportInbBtn = new Button();
        exportInbBtn.Text = "Export Inventury";
        exportInbBtn.Click += new EventHandler(myButton_ServerClick);
        ph.Controls.Add(exportInbBtn);
        exportInbBtn.ID = "exportInbBtn";

        return ph;
    }

Methods Page_Load and Page_Init in ASP page are empty. Problem is, when the user click at the button exportInbBtn (with text: "Export Inventury"), the click event myButton_ServerClick will not rise. Only web page refresh. I ready some answers, but I can′t figure how easily solve this problem.

ASP页面中的Page_Load和Page_Init方法为空。问题是,当用户单击按钮 exportInbBtn(带有文本:“Export Inventury”)时,单击事件 myButton_ServerClick 不会上升。只刷新网页。我准备了一些答案,但我无法想象解决这个问题有多容易。

采纳答案by Rudis

If you fire *myButton_ServerClick* event, postback is invoked and ASP.Net want to fire called event, but your control is not added to the page, that's why ASP.Net ignore this event.

如果你触发 *myButton_ServerClick* 事件,回发被调用并且 ASP.Net 想要触发被调用的事件,但是你的控件没有添加到页面,这就是 ASP.Net 忽略这个事件的原因。

After postback and before event to be fired, you must add your control again and after them, event will be invoked.

在回发之后和触发事件之前,您必须再次添加您的控件,在它们之后,将调用事件。

Update

更新

Something like this

像这样的东西

Page:

页:

<asp:Button runat="server" ID="btnTest" Text="Add control" OnClick="btnTest_Click"/>
<asp:Label runat="server" ID="result"></asp:Label>
<asp:HiddenField runat="server" ID="controlLoaded"/>
<asp:PlaceHolder runat="server" ID="phTest"></asp:PlaceHolder>

Code behind:

后面的代码:

    protected void Page_Load(object sender, EventArgs e)
    {
        if (controlLoaded.Value == "1")
        {
            AddControl();
        }
    }

    protected void btnTest_Click(object sender, EventArgs e)
    {
        AddControl();
    }

    protected void myButton_ServerClick(object sender, EventArgs e)
    {
        result.Text = "OK";
    }

    public object getControl()
    {
        var ph = new PlaceHolder();

        var exportInbBtn = new Button();
        exportInbBtn.Text = "Export Inventury";
        exportInbBtn.Click += new EventHandler(myButton_ServerClick);
        ph.Controls.Add(exportInbBtn);
        exportInbBtn.ID = "exportInbBtn";

        return ph;
    }

    private void AddControl()
    {
        var actualObject = (PlaceHolder)getControl();
        phTest.Controls.Add(actualObject);
        controlLoaded.Value = "1";
    }

回答by KhaledDev

page.aspx

页面.aspx

<asp:PlaceHolder ID="PlaceHolder1" runat="server" ></asp:PlaceHolder>

page.aspx.vb

页面.aspx.vb

Private Sub CreateControl()
    Dim vw As Control
    vw = CType(LoadControl("~/View01.ascx"), View01)
    vw.ID = "View_Dyn"
    PlaceHolder1.Controls.Clear()
    PlaceHolder1.Controls.Add(vw)
End Sub

Recreate it every postback

每次回发重新创建它

Private Sub Page_Init(sender As Object, e As System.EventArgs) Handles Me.Init
    CreateControl()
End Sub

回答by Naresh Mittal

Please note that UserControls are added by the below method - // First create the instance of user control var control = Page.LoadControl("~/folder1/UserControl1.ascx")

请注意,UserControls 是通过以下方法添加的 - // 首先创建用户控件的实例 var control = Page.LoadControl("~/folder1/UserControl1.ascx")

// Now add this control to placeholder as - placeHolder1.Controls.Add(control);

// 现在将此控件添加到占位符作为 - placeHolder1.Controls.Add(control);

I hope this helps.

我希望这有帮助。