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

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

    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?

    Dave R, Mar 17, 2005
    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

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

    For VB, have a look at AddHandler.

    Jason Bentley
    Jason Bentley, Mar 17, 2005
    1. Advertisements

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 (here). After that, you can post your question and our members will help you out.