0
votes

I dynamically load one of many user controls based on which tab the user clicks. I store the selected tab index in view state. The user control has a checkbox on it that fires an OnChanged event. My problem is that if I load the user control in Page_Load, the checkbox event doesn't fire because the control didn't get created in time. If I load the user control in Page_Init, the page doesn't know which user control to load I believe because the ViewState isn't loaded yet. How can I store which user control to load AND get the events on the user control to fire?

1
I'll just add that you can get the checkbox event to fire if you load it dynamically in Page_Load of the containing page/control. If it is not firing then likely something else is going wrong. The most common problem I've seen that creates this problem is not setting the id of the checkbox to the same id everytime you initialize itDustin Hodges

1 Answers

1
votes

The best strategy I've found has been to create all the controls in the Page_Init event, and set their Visible property to false (in Page_Load in your case) if they aren't supposed to be present on the rendered page.

Edit

Another option is to determine which controls to load based on some other criterion (besides ViewState). For example, if you make the current tab one of your query string parameters, that data will be available on the request during Page_Init.