Handling events of controls created at runtime

K

Klaus Jensen

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. :)
 
Y

Yuriy Solodkyy

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
 

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

Ask a Question

Members online

Forum statistics

Threads
473,755
Messages
2,569,535
Members
45,007
Latest member
obedient dusk

Latest Threads

Top