ASP.NET动态添加用户控件来占位符不火的Click事件中,只有的Page_Load [英] ASP.NET dynamically adding UserControl to PlaceHolder, not fire Click Event, only Page_Load

查看:186
本文介绍了ASP.NET动态添加用户控件来占位符不火的Click事件中,只有的Page_Load的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在我的ASP.Net的网页我有占位符按钮。当用户点击这个按钮,我从我的接口方法getControl添加一些用户控件的占位符。 code:

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

方法getControl:

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

方法的Page_Load和Page_Init在ASP页面是空的。问题是,当在按钮exportInbBtn用户点击(文字:导出Inventury),单击事件myButton_ServerClick不会上涨。只有网页的刷新。是否准备好了一些答案,但我不能图如何轻松解决这个问题。

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.

推荐答案

如果你火* myButton_ServerClick *事件,回传被调用,ASP.Net想火称为事件,但你的控制未添加到页面,这就是为什么ASP.Net忽略此事件。

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.

更新

像这样

页:

<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:

    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动态添加用户控件来占位符不火的Click事件中,只有的Page_Load的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆