开发者

How to control which user control to load in a way that allows user control events to fire

I dynamically load one of many user controls based on which tab the user clicks. I st开发者_JAVA百科ore 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?


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.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜