event handlers for dynamically-added controls?

Discussion in 'ASP .Net Web Controls' started by Dave R, Mar 17, 2005.

  1. Dave R

    Dave R Guest

    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
     
    Dave R, Mar 17, 2005
    #1
    1. Advertising

  2. 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
     
    Jason Bentley, Mar 17, 2005
    #2
    1. Advertising

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

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. MS Newsgroups
    Replies:
    5
    Views:
    1,526
    Anders Borum
    Dec 13, 2003
  2. Jeffrey Todd
    Replies:
    1
    Views:
    4,131
    Peter Blum
    Jun 2, 2005
  3. =?Utf-8?B?TWlrZSBSYW5k?=

    Event handlers and dynamically created controls

    =?Utf-8?B?TWlrZSBSYW5k?=, Jan 25, 2007, in forum: ASP .Net
    Replies:
    2
    Views:
    433
    =?Utf-8?B?TWlrZSBSYW5k?=
    Jan 26, 2007
  4. Dica
    Replies:
    1
    Views:
    412
    bruce barker
    Jun 20, 2007
  5. John Kilgo

    Adding Event Handler to Dynamically added Controls

    John Kilgo, Apr 25, 2005, in forum: ASP .Net Web Controls
    Replies:
    4
    Views:
    208
    John Kilgo
    Apr 26, 2005
Loading...

Share This Page