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

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 ??
 
S

Sherif ElMetainy

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
 
U

Umut Tezduyar

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
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,744
Messages
2,569,483
Members
44,901
Latest member
Noble71S45

Latest Threads

Top