How to associate an event handler with an ASP.NET control created at runtime?

Good morning everybody.

I have a question related to event management and handling. Suppose 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 will be fine. But when I try to start loadLinkButton by pressing a simple button, a link button will be created, but the event will not be processed.

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

Is there any way to solve it? Or loadLinkButton should always be restored to Page_Load , Page_init , etc.

+4
source share
5 answers

When working with dynamic controls, I always add a control to Page_Init , because loading in view mode will occur immediately after Init. If you add it to Page_Load, chances are that you will lose the viewstate. Just make sure you provide a unique management identifier.

+3
source

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

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

Whenever a page loads, it extracts the source of the event from this field and then determines which event to trigger. Now all this is great for controls added with markup, because the entire control tree is regenerated for each request.

However, your control has been added only once. When the callback occurs, your control no longer exists as a server control in the tree, and therefore the event never fires.

An easy way to avoid this is to ensure that your dynamic controls are added every time the page loads, either through the Page_Init event or the Page_Load event.

+3
source

You're right. This is the expected behavior. Page_Load and Page_Init will be events to which you must add it.

0
source

This would be because when you click on a dynamically generated link, you are posting back to the server. There you make a completely new pageload, but your original buttonclick (which generates the link) never occurred, so the linkbutton was never created and the event could not be thrown.

An alternative is to add a link button that you add dynamically to your page statically, with Visible = false. And when you press another button, make it visible.

0
source

I'm not quite sure what the problem you are facing, but you should put the dynamic control 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!"; } } 
-1
source

All Articles