Page Life Cycle Events
During each stage of Life cycle Page raises events that developers can handle to write code. Explained below are most frequently used events during Page Life Cycle. It will help you to understand significance of every step.
PreInit – Raised after start stage and before Init stage begins.
• Sets Page properties such as IsPostBack, IsCallBack, IsCrossPagePostBack.
• Dynamically create controls.
• Set Master page, Theme Properties dynamically.
• Set Profile property value.
Tip - Do not set control values here as these values may be overwritten in case of PostBack when restored from view state.
Init –
• All controls have been initialized and skin settings are applied.
• Use this event to read or Initialize control properties.
• Init event of individual controls occur before the Init event of the Page.
InitComplete –
• It denotes the end of Page initialization stage.
• Tracking of ViewState changes is turned on. View state tracking enables controls to persist values that are added programmatically.
• Also used to make changes to Control's view state.
PreLoad –
• Raised after page loads view state for itself & all controls after processing postback data which is present in Page's Request object.
Load –
• Page's "OnLoad" method is called & it recursively calls Load method of all child controls.
• Used to establish DB connection, initialize required objects, set control properties etc.
Control Events –
• This event is used to handler control event & performs some processing based on an event being fired.
• In case of postback request make sure to verify IsValid Property of Page & control before processing any information.
LoadComplete –
• After event handling stage used to perform actions that require all controls on page to be loaded.
PreRender –
• All controls are created, Page Object raises PreRender event of it and then raises it for each child control recursively.
• Used to make final changes to content of the page and its controls.
PreRenderComplete –
• Raised for each data bound control whose DataBind method is called.
SaveState –
• Changes at this stage will affect rendering but will not be retained during successive postbacks.
• View state & Control state for Page and controls is saved.
Render –
• All ASP.NET server controls have Render method that writes out the control's markup to send to the browser. This Render method is overridden in case if Custom controls.
• Page Object calls this method on each control.
Unload –
• All child controls are unloaded first and then page.
• Final clean up is done such as closing DB connections, open files etc.
GENERAL PAGE LIFE-CYCLE STAGE:-
------------------------------------------------
In general terms, the page goes through the stages outlined in the following point. In addition to the page life-cycle stages, there are application stages that occur before and after a request but are not specific to a page.
Some parts of the life cycle occur only when a page is processed as a postback. For postbacks, the page life cycle is the same during a partial-page postback (as when you use an UpdatePanel control) as it is during a full-page postback.
a) Page request:- The page request occurs before the page life cycle begins. When the page is requested by a user, ASP.NET determines whether the page needs to be parsed and compiled (therefore beginning the life of a page), or whether a cached version of the page can be sent in response without running the page.
b) Start:- In the start stage, page properties such as Request and Response are set. At this stage, the page also determines whether the request is a postback or a new request and sets the IsPostBack property. The page also sets the UICulture property.
c) Initialization:- During page initialization, controls on the page are available and each control's UniqueID property is set. A master page and themes are also applied to the page if applicable. If the current request is a postback, the postback data has not yet been loaded and control property values have not been restored to the values from view state.
d) Load:- During load, if the current request is a postback, control properties are loaded with information recovered from view state and control state.
e) Postback event handling:- If the request is a postback, control event handlers are called. After that, the Validate method of all validator controls is called, which sets the IsValid property of individual validator controls and of the page. (There is an exception to this sequence: the handler for the event that caused validation is called after validation.)
f) Rendering:- Before rendering, view state is saved for the page and all controls. During the rendering stage, the page calls the Render method for each control, providing a text writer that writes its output to the OutputStream object of the page's Response property.
g) Unload:- The Unload event is raised after the page has been fully rendered, sent to the client, and is ready to be discarded. At this point, page properties such as Response and Request are unloaded and cleanup is performed.