event handlers for dynamically-added controls?


D

Dave R

Anyone know how to wire up an event handler for controls added to an ASP.NET
page at run-time? Of course, at design-time this is easy: just enter the
method's name in the event tab of the control's properties. But what if I
do...

LinkButton lb = new LinkButton();
lb.Text = "Click me now!";
cell.Controls.Add(lb);

I want to create a handler for lb's Click event, like...
lb.Click = "MyHandler";
....but that syntax is illegal. When you click the button, it properly posts
back, but I can't infer in Page_Load() the reason (i.e., the originator) of
the action. Can't find anything in the on-line docs about this. Any ideas?

Thanks,
:-David
 
Ad

Advertisements

J

Jason Bentley

David, have a look at the generated code in your web form when you
double click a button. It is different for each language but that is
how you will be adding event handlers to your dynamically created
controls.

An example in C# is:
this.Button1.Click += new System.EventHandler(this.Button1_Click);

For VB, have a look at AddHandler.

Jason Bentley
http://geekswithblogs.net/jbentley
 

Top