ASP.NET 将 UserControl 动态添加到 PlaceHolder,不会触发 Click 事件,只有 Page_Load

ASP.NET dynamically adding UserControl to PlaceHolder, not fire Click Event, only Page_Load(ASP.NET 将 UserControl 动态添加到 PlaceHolder,不会触发 Click 事件,只有 Page_Load)
本文介绍了ASP.NET 将 UserControl 动态添加到 PlaceHolder,不会触发 Click 事件,只有 Page_Load的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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:

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

Method 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.

解决方案

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.

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";
    }

这篇关于ASP.NET 将 UserControl 动态添加到 PlaceHolder,不会触发 Click 事件,只有 Page_Load的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!

本站部分内容来源互联网,如果有图片或者内容侵犯您的权益请联系我们删除!

相关文档推荐

DispatcherQueue null when trying to update Ui property in ViewModel(尝试更新ViewModel中的Ui属性时DispatcherQueue为空)
Drawing over all windows on multiple monitors(在多个监视器上绘制所有窗口)
Programmatically show the desktop(以编程方式显示桌面)
c# Generic Setlt;Tgt; implementation to access objects by type(按类型访问对象的C#泛型集实现)
InvalidOperationException When using Context Injection in ASP.Net Core(在ASP.NET核心中使用上下文注入时发生InvalidOperationException)
LINQ many-to-many relationship, how to write a correct WHERE clause?(LINQ多对多关系,如何写一个正确的WHERE子句?)