Let's say that I have a page called Test.aspx which contains a button
called Button1 that has a corresponding Button1_Click event handler.
Let's also say that I have a control called TestControl.ascx which
contains another button called Button2. TestControl is added
dynamically due to Button1 click. When Button1 is clicked the page
execution happens as follows:
- Page_Load
- Button1_Click
If I have code inside Button1_Click to dynamically add the TestControl,
the next time the page is reloaded due to some other actions, say as a
result of Button2_Click in TestControl, the TestControl itself is not
loaded anymore. So I needed to have the logic in the Page_Load event
to check for a session variable to check if the TestControl should be
loaded or not. That way the TestControl is loaded as long as the
session variable is "true". On Button1_Click I set session variable to
"true" and do a Server.Transfer to itself. The Page_Load event looks
for that session variable and if it is set to true it loads the user
control dynamically.
Is there a better way of doing this? Thank you very much.