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

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

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,732
    Anders Borum
    Dec 13, 2003
  2. Jeffrey Todd
    Replies:
    1
    Views:
    4,393
    Peter Blum
    Jun 2, 2005
  3. David Hubbard
    Replies:
    2
    Views:
    769
    David Hubbard
    Jan 17, 2006
  4. Michael Dawson
    Replies:
    3
    Views:
    624
    Karl Seguin [MVP]
    Feb 22, 2006
  5. =?Utf-8?B?TWlrZSBSYW5k?=

    Event handlers and dynamically created controls

    =?Utf-8?B?TWlrZSBSYW5k?=, Jan 25, 2007, in forum: ASP .Net
    Replies:
    2
    Views:
    543
    =?Utf-8?B?TWlrZSBSYW5k?=
    Jan 26, 2007
  6. Dica
    Replies:
    1
    Views:
    498
    bruce barker
    Jun 20, 2007
  7. ENathan

    existing controls overlaying dynamically added controls

    ENathan, Jan 10, 2005, in forum: ASP .Net Web Controls
    Replies:
    2
    Views:
    321
    Steve C. Orr [MVP, MCSD]
    Jan 10, 2005
  8. John Kilgo

    Adding Event Handler to Dynamically added Controls

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