473,326 Members | 2,813 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,326 software developers and data experts.

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

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 ??
Nov 17 '05 #1
2 1242
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
"Umut Tezduyar" <ut*******@onmap.com.tr> wrote in message
news:uk**************@tk2msftngp13.phx.gbl...
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 ??

Nov 17 '05 #2
Thanks, that has solved my problem !!

"Sherif ElMetainy" <el*************@wayout.net.NOSPAM> wrote in message
news:%2****************@TK2MSFTNGP12.phx.gbl...
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
"Umut Tezduyar" <ut*******@onmap.com.tr> wrote in message
news:uk**************@tk2msftngp13.phx.gbl...
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 ??


Nov 17 '05 #3

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

8
by: Ashish Shridharan | last post by:
Hi All I have been trying to add a control to the header cell of a datagrid on my ASP.NET page. These controls are defined in the HTML as ASP.NET web controls. They are being added into the...
6
by: vbMark | last post by:
If I have a control, for example a CheckedListBox, how do I add and event to code, for example that a box has been checked by the user? Thanks
5
by: Josema | last post by:
Hi, I would need that when i click the button the code of the Onclick method executes first, and then the onInit Method... Its possible make this? -- Thanks Regards. Josema
3
by: Big D | last post by:
I have a class that inherits from System.Web.Ui.Usercontrol... all is basically is is a button which is created progammatically such as: public abstract class BaseFrontEndEditor :...
0
by: Alessandro Rinaldi | last post by:
Hi all, what's wrong in this ? I added a simple Control with viewstate (ie : label, or my User control... ) dinamically in the controls collection of a TD cell created dynamically too, i lose...
6
by: Steve Caliendo | last post by:
Hi, I'm creating 5 ImageButton controls in the panel control, and I have a unique ID specified for each one. When I click on any one of them, the Page_Load executes (Of course), but how do I...
7
by: Jed | last post by:
I have a web service method that returns an array of custom objects marked serializeable with fully described public properties. When I bind the results to a DataGrid I can access the properties...
1
by: sianan | last post by:
I tried to use the following example, to add a checkbox column to a DataGrid in an ASP.NET application: http://www.codeproject.com/aspnet/datagridcheckbox.asp For some reason, I simply CAN'T get...
0
by: Demetri | last post by:
I have created a web control that can be rendered as either a linkbutton or a button. It is a ConfirmButton control that allows a developer to force a user to confirm if they intended to click it...
0
by: ryjfgjl | last post by:
ExcelToDatabase: batch import excel into database automatically...
0
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
0
by: Vimpel783 | last post by:
Hello! Guys, I found this code on the Internet, but I need to modify it a little. It works well, the problem is this: Data is sent from only one cell, in this case B5, but it is necessary that data...
0
by: jfyes | last post by:
As a hardware engineer, after seeing that CEIWEI recently released a new tool for Modbus RTU Over TCP/UDP filtering and monitoring, I actively went to its official website to take a look. It turned...
0
by: ArrayDB | last post by:
The error message I've encountered is; ERROR:root:Error generating model response: exception: access violation writing 0x0000000000005140, which seems to be indicative of an access violation...
1
by: PapaRatzi | last post by:
Hello, I am teaching myself MS Access forms design and Visual Basic. I've created a table to capture a list of Top 30 singles and forms to capture new entries. The final step is a form (unbound)...
1
by: Defcon1945 | last post by:
I'm trying to learn Python using Pycharm but import shutil doesn't work
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome former...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.