AddHandler and dynamically generated controls

Discussion in 'ASP .Net Web Controls' started by Greg, Feb 11, 2008.

  1. Greg

    Greg Guest

    Hello,

    I have a form where, on a button's click event, I add some link buttons to
    the page. I have an event handler for these link buttons' click event, which
    I wire up to the buttons using AddHandler.

    The problem is, I can't get the event handler to fire. When I click on one
    of the link buttons, the link buttons vanish from the page, and the click
    event handler is never fired.

    I'm not sure if I'm putting AddHandler in the wrong place (I've tried the
    routine where I generate the link buttons, as well as every event on Page and
    the link buttons' parent control - a PlaceHolder - that seemed even remotely
    likely) or if there's something else entirely that I'm missing. Any help
    with getting Click to fire for these link buttons would be greatly
    appreciated.

    Thanks you!
    Greg
     
    Greg, Feb 11, 2008
    #1
    1. Advertising

  2. Greg

    Angel Guest

    If you post some code it would be helpful.
    --
    aaa


    "Greg" wrote:

    > Hello,
    >
    > I have a form where, on a button's click event, I add some link buttons to
    > the page. I have an event handler for these link buttons' click event, which
    > I wire up to the buttons using AddHandler.
    >
    > The problem is, I can't get the event handler to fire. When I click on one
    > of the link buttons, the link buttons vanish from the page, and the click
    > event handler is never fired.
    >
    > I'm not sure if I'm putting AddHandler in the wrong place (I've tried the
    > routine where I generate the link buttons, as well as every event on Page and
    > the link buttons' parent control - a PlaceHolder - that seemed even remotely
    > likely) or if there's something else entirely that I'm missing. Any help
    > with getting Click to fire for these link buttons would be greatly
    > appreciated.
    >
    > Thanks you!
    > Greg
     
    Angel, Feb 11, 2008
    #2
    1. Advertising

  3. Greg

    Greg Guest

    Well, I seem to have answered my own question.

    For some reason, controls keep track of all property changes you make
    between requests, but they DON'T keep track of controls that you add to their
    Controls collection. So basically, when I clicked one of the LinkButtons I
    had created in the code previously, it did a postback, but it didn't fire the
    Click event, because the control no longer existed! This also explains why
    the LinkButtons all vanished from the page after the postback.

    So to get around this, I had to keep track of the link buttons I had created
    using the ViewState. Then, on postback, I had to check the ViewState to see
    if I'd created any link buttons previously. If so, I had to recreate them,
    and link the Click event to the event handler again with AddHandler. And
    this worked!


    "Angel" wrote:

    > If you post some code it would be helpful.
    > --
    > aaa
    >
    >
    > "Greg" wrote:
    >
    > > Hello,
    > >
    > > I have a form where, on a button's click event, I add some link buttons to
    > > the page. I have an event handler for these link buttons' click event, which
    > > I wire up to the buttons using AddHandler.
    > >
    > > The problem is, I can't get the event handler to fire. When I click on one
    > > of the link buttons, the link buttons vanish from the page, and the click
    > > event handler is never fired.
    > >
    > > I'm not sure if I'm putting AddHandler in the wrong place (I've tried the
    > > routine where I generate the link buttons, as well as every event on Page and
    > > the link buttons' parent control - a PlaceHolder - that seemed even remotely
    > > likely) or if there's something else entirely that I'm missing. Any help
    > > with getting Click to fire for these link buttons would be greatly
    > > appreciated.
    > >
    > > Thanks you!
    > > Greg
     
    Greg, Feb 12, 2008
    #3
    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. Steve Caliendo
    Replies:
    2
    Views:
    475
    Mohamed El Ashmawy
    Mar 1, 2004
  2. Nathan Sokalski
    Replies:
    3
    Views:
    3,067
    Nathan Sokalski
    Jun 9, 2005
  3. bill
    Replies:
    2
    Views:
    584
  4. Nathan Sokalski

    Problem using AddHandler for dynamically created WebControls

    Nathan Sokalski, Jun 8, 2005, in forum: ASP .Net Building Controls
    Replies:
    3
    Views:
    138
    Nathan Sokalski
    Jun 9, 2005
  5. Nathan Sokalski

    Problem using AddHandler for dynamically created WebControls

    Nathan Sokalski, Jun 8, 2005, in forum: ASP .Net Web Controls
    Replies:
    3
    Views:
    138
    Nathan Sokalski
    Jun 9, 2005
Loading...

Share This Page