Handling events of controls created at runtime

Discussion in 'ASP .Net' started by Klaus Jensen, Jun 21, 2007.

  1. Klaus Jensen

    Klaus Jensen Guest

    Subject: Handling events of controls created at runtime

    I need to render some buttons at runtime based on a database, tie events to
    them, and handle the events when they fire.

    In Page_Load I create the controls (for example a delete-button), add them
    to a placeholder, add eventhandlers, and the controls are displayed as
    expected.

    I click the button, the event fires as I wanted, I delete a record in the
    database, and now I want to re-render the entire thing to reflect the
    changes in the database.

    This is where my problems start...

    If I clear the placeholder and render the buttons/controls again, the
    eventhandlers work. I click a button, no event is fired.


    I currently work around this by response.redirecting to the page again - and
    this created an illusion of what I want, but is not the right way to handle
    this.


    Please note, that this is a simplified example - obviously there was no need
    to generate controls at runtime, if it could be solved by using a datagrid
    or something like that. :)
     
    Klaus Jensen, Jun 21, 2007
    #1
    1. Advertising

  2. Hi,

    Do you assign ID to your controls when you create them? If you don't, try
    assigning ID in a way that button for the same row has the same ID after
    you clear placheholder an add controls back. When you clear placeholder
    and add controls back, they get new IDs. However, on the next postback they
    get their old IDs.

    See http://www.singingeels.com/Articles/Dynamically_Created_Controls_in_ASPNET.aspx.
    I think I described the problem you expirience.

    -yuriy


    > Subject: Handling events of controls created at runtime
    >
    > I need to render some buttons at runtime based on a database, tie
    > events to them, and handle the events when they fire.
    >
    > In Page_Load I create the controls (for example a delete-button), add
    > them to a placeholder, add eventhandlers, and the controls are
    > displayed as expected.
    >
    > I click the button, the event fires as I wanted, I delete a record in
    > the database, and now I want to re-render the entire thing to reflect
    > the changes in the database.
    >
    > This is where my problems start...
    >
    > If I clear the placeholder and render the buttons/controls again, the
    > eventhandlers work. I click a button, no event is fired.
    >
    > I currently work around this by response.redirecting to the page again
    > - and this created an illusion of what I want, but is not the right
    > way to handle this.
    >
    > Please note, that this is a simplified example - obviously there was
    > no need to generate controls at runtime, if it could be solved by
    > using a datagrid or something like that. :)
    >
     
    Yuriy Solodkyy, Jun 22, 2007
    #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. Nathan Sokalski

    Handling Events for Dynamically Created Controls

    Nathan Sokalski, May 23, 2005, in forum: ASP .Net
    Replies:
    2
    Views:
    1,013
    Herfried K. Wagner [MVP]
    May 23, 2005
  2. Nathan Sokalski
    Replies:
    1
    Views:
    314
    bruce barker
    Feb 27, 2008
  3. Lalit
    Replies:
    2
    Views:
    228
  4. Nathan Sokalski

    Handling Events for Dynamically Created Controls

    Nathan Sokalski, May 23, 2005, in forum: ASP .Net Web Controls
    Replies:
    2
    Views:
    138
    Herfried K. Wagner [MVP]
    May 23, 2005
  5. Nathan Sokalski

    Handling events triggered by dynamically created controls

    Nathan Sokalski, Feb 27, 2008, in forum: ASP .Net Web Controls
    Replies:
    1
    Views:
    137
    bruce barker
    Feb 27, 2008
Loading...

Share This Page