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

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

No members online now.

Forum statistics

Threads
473,769
Messages
2,569,580
Members
45,055
Latest member
SlimSparkKetoACVReview

Latest Threads

Top