Excerpt from another page I found helpful:
http://msdn2.microsoft.com/en-us/library/ms972976.aspx
So, dynamically added controls must be programmatically added to the Web page on each and every page visit. The best time to add these controls is during the initialization stage of the page life cycle, which occurs before the load view state stage. That is, we want to have the control hierarchy complete before the load view state stage arrives. For this reason, it is best to create an event handler for the
Page
class's Init
event in your code-behind class, and add your dynamic controls there.Note You may be able to get away with loading your controls in theShortcut Summary:Page_Load
event handler and maintaining the view state properly. It all depends on whether or not you are setting any properties of the dynamically loaded controls programmatically and, if so, when you're doing it relative to theline. A thorough discussion of this is a bit beyond the scope of this article, but the reason it may work is because the
Controls.Add(
dynamicControl
)
Controls
property'sAdd()
method recursively loads the parent's view state into its children, even though the load view state stage has passed.
1. Object Initialization
2. Load Viewstate Data (void LoadViewState( object o ) )
3. LoadPostData Processes Postback Data
4. Object Load (void onload(EventArgs e) )
5. Raise PostBack Change Events
6. Process Client-Side PostBack Event (void RaisePostBackEvent (string eventArgument) )
7. Prerender the Objects
8. ViewState Saved (void RaisePostDataChangeEvent() )
9. Render To HTML
10. Disposal (void OnPreRender( EventArgs e ) )
No comments:
Post a Comment