Little things about the Page Lifecycle
PreInit: If you want to change the masterpage programatically, this is the event handler where it should be done: http://msdn.microsoft.com/en-us/library/c8y19k6h.aspx. Trying to change the MasterPage in any other event will cause an InvalidOperationException. This is because the masterpage rearranges the hierarchy of controls.
This is also the event handler where themes should be set programatically. If you try to change themes elsewhere, this is what you get: Invalid operation exception -
The 'Theme' property can only be set in or before the 'Page_PreInit' event. http://msdn.microsoft.com/en-us/library/tx35bd89.aspx
Dynamic controls have to be recreated each time in PreInit. If dynamic controls are not recreated (on each postback), they do not appear in the page. If dynamic controls are created after PreInit method, the themes are not properly set.
Init: You can init control properties here. This is generally not used at all. This is because of the next event - LoadViewState
LoadViewState: LoadViewState loads control properties from the ViewState. This usually works in the case of postbacks.
Load: Do databinding of controls here manually. Initialize controls (for first time). Most initialization is done for the first time (IsPostback=false)
UserControl Load: Each UserControls Load is called. Here user controls are initialized.
LoadComplete: If you want to make some initializations based on UserControl's state, this is where to do it. Very rarely used. This is because UserControls initialize from ViewState. This is usually used when EnableViewState=false
Validation: If any validator controls are there in the page, this event is fired. After this event, Page.IsValid property is set. This is fired for postbacks
RaisePostBackEvent: This is the postback event handler where you write your code. Before writing any code, check if the page is valid. (IsValid=true)
PreRender: Used to emit custom scripts.
Automatic DataBinding: If you are using SqlDataSource, XmlDataSource etc, this is where automatic data binding happens. Automatic DataBinding happens only when IsPostBack=false (for the first time). In Postbacks, the ViewState loads the data.
SaveViewState: Saves the control properties into ViewState (which goes into the Response)
Render: Renders the control into the Response stream as HTML + Javascript.
Unload: Page and all its objects are released from memory.
Category : ASP.NET


