Why i cannot bind the event handler of the control that i dinamically added ?

  • Thread starter Thread starter Umut Tezduyar
  • Start date Start date
U

Umut Tezduyar

public class WebForm1 : System.Web.UI.Page

{

private void Page_Load(object sender, System.EventArgs e)

{

}

protected override void OnPreRender(EventArgs e)

{

LinkButton btn = new LinkButton ();

btn.Text = "Click me!";

btn.Click +=new EventHandler(btn_Click);

Control c = Page.FindControl ("Form1");

c.Controls.Add (btn);

}

private void btn_Click(object sender, EventArgs e)

{

int i = 0; // I cannot execute this line

}


}



I have added my class definition. On the html page, there is no extra
control. I add my link button dynamically, but i cannot fire the click
event. Is there a "control life cycle" dislogic. Or is there something that
i dont know ??
 
Hello

ASP.NET handles postback events before the prerender event. So you are
registering your method in the click event after postback events are
handled.
Adding the dynamic control in Load event would solve it because it occurs
before handling postback events

Best regards,

Sherif
 
Thanks, that has solved my problem !!

Sherif ElMetainy said:
Hello

ASP.NET handles postback events before the prerender event. So you are
registering your method in the click event after postback events are
handled.
Adding the dynamic control in Load event would solve it because it occurs
before handling postback events

Best regards,

Sherif
 
Back
Top