Handling Events from a Dynamically Added Control
Posted on 2009-04-14
Handling an event that is raised from a dynamically-added control is a do-able task for me. What I would like some assistance with is the following:
A user goes to my web application and clicks an "Add Control" button to add a user control to the page. This control contains a "Change Text" button that I would like to do something when clicked.
The way that I am handing the "Add Control" button is by adding the user control to an array of user controls (stored as a Session variable), and adding all controls in the array to a Panel each time the page is Initialized. This way I don't lose the controls each postback.
Here's the problem. When the "Change Text" button is clicked, it re-Initializes (adding the user controls again) and re-Loads the page, without raising the "Change Text" click event. How can I make this happen?
I've already tried the following:
- Creating a public withevents "Change Text" button in the user control, and using the handles clause to map it to an event handler in the user control's code
- Adding the "Change Text" button when the user control is initialized, and using the AddHandler to map its click event to the correct event handler
- Creating a Public Event in the user control to be raised when the "Change Text" button is clicked, and catching that event on the page side
- Only adding a "Change Text" button, instead of adding a user control that contains a "Change Text" button
- Studying ASP.NET's life cycle to see where things go arye - I think this might be why it's not working, and might not even be possible.
Any and all suggestions are welcome. Thank you!