开发者

How do I attach an event handler to an ASP.NET control created at runtime?

Good morning everybody.

I have a question connected with controls and event handling. Lets say I want to create a LinkButton.

protected void loadLinkButton()
{
    ContentPlaceHolder content = (ContentPlaceHolder)this.Master.FindControl开发者_如何学编程("MainContent");
    LinkButton lnk = new LinkButton();
    lnk.ID = "lnikBtn";
    lnk.Text = "LinkButton";
    lnk.Click += new System.EventHandler(lnk_Click);
    content.Controls.Add(lnk);
}

Here is the event handler:

protected void lnk_Click(object sender, EventArgs e)
{
    Label1.Text = "ok!";
}

If I run the loadLinkButton function inside Page_Load everything is ok. But when I try to run the loadLinkButton by clicking simple button, link button is created but event is not handled.

protected void Button1_Click(object sender, EventArgs e)
{
    loadLinkButton();
}

I there any way to solve it? Or loadLinkButton must always regenerated on Page_Load, Page_init etc.


When working with dynamic controls, I always add the control in Page_Init, because viewstate loading will happen right after Init. If you add it to Page_Load, there is a chance that you will lose viewstate. Just make sure you provide a unique control ID.


It is important to know how ASP.Net determines which events to invoke. The source of each event is passed using a hidden field:

<input type="hidden" name="__EVENTTARGET" value="" />

Whenever the page loads, it pulls in the source of the event from that field and then determines which event to invoke. Now this all works great for controls added through markup because the entire control tree is regenerated on every request.

However, your control was only added once. When a Postback occurs, your control no longer exists as a Server control in the tree, and therefore the event never fires.

The simply way to avoid this is to make sure your Dynamic Controls are added every time the page loads, either through the Page_Init event, or the Page_Load event.


You are right. This is the expected behavior. Page_Load and Page_Init would be the events where you should be adding it.


That would be because when you click your dynamically generated linkbutton, you do a postback to the server. There you do an entirely new pageload, but your original buttonclick (that generates the link) never happened now, so the linkbutton is never made, and the event can not be thrown.

An alternative is to add the linkbutton you add dynamically, to your page statically, with Visible = false. And when you click the other button, make it visible.


I am not exactly sure what problem you are facing but you should put the dynamic controls code in Page_Init as suggested by @johnofcross:

public partial class WebForm1 : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {

        }

        protected void Page_Init(object sender, EventArgs e)
        {
            CreateControls();
        }

        private void CreateControls()
        {
            var lb = new LinkButton();
            lb.Text = "Click Me";
            lb.Click += lb_Click;

            ph.Controls.Add(lb);
            ph.DataBind();
        }

        void lb_Click(object sender, EventArgs e)
        {
            lblMessage.Text = "Button is clicked!"; 
        }
    }
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜