When the page is requested by a user, ASP.NET
determines whether the page needs to be parsed and compiled, or whether a
cached version of the page can be sent in response without running the page. At
each stage of the page life cycle, the page raises some events, which could be
coded.
Following are the
page life cycle events:
PreInit: PreInit
is the first event in page life cycle and raised before the initialization
stage begins. Use this event for the following:
- Check the IsPostBack property to determine whether this is the first time the page is being processed. If the request is a postback, the values of the controls have not yet been restored from view state. If you set a control property at this stage, its value might be overwritten in the next event.
- The IsCallback and IsCrossPagePostBack properties have also been set at this time.
- Create or re-create dynamic controls.
- Set a master page & theme property dynamically.
- Read or set profile property values.
This event can be handled by overloading the
OnPreInit method or creating a Page_PreInit handler.
Master page doesn't have PreInit method because Master
pages are derived from Control class as seen in below hierarchy:
Init:
This event is raised after all controls have been initialized and any skin
settings have been applied. Init event initializes the control property and the
control tree is built. The Init event of individual controls occurs before the
Init event of the page. Use this event to read or initialize control
properties.
This event can be handled by overloading the
OnInit method or creating a Page_Init handler.
Order:
1. Master
page controls Init event
2. Content
controls Init event.
3. Master
page Init event.
4. Content
page Init event.
InitComplete: This event is raised at the end of the page's initialization
stage. InitComplete event allows tracking of view state. All the controls turn
on view-state tracking.
View state tracking enables controls to
persist any values that are programmatically added to the ViewState collection.
Use this event to make changes to view state that you want to make sure are
persisted after the next postback.
PreLoad: This
event is raised after the page loads view state for itself and all controls
& occurs before the post back data is loaded in the controls
This event can be handled by overloading the
OnPreLoad method or creating a Page_PreLoad handler.
Master
page doesn't have PreLoad method.
Load:
The Load event is raised for the page first and then recursively does the same
for each child control until the page and all controls are loaded.
This event can be
handled by overloading the OnLoad method or creating a Page_Load handler.
Order:
1. Content
page Load event.
2. Master
page Load event.
3. Master
page controls Load event.
4. Content
page controls Load event.
Load Complete: This event is raised at the end of the
event-handling stage. Use this event for tasks that require that all other
controls on the page be loaded.
This event can be handled by overloading the
OnLoadComplete method or creating a Page_LoadComplete handler.
Master
page doesn't have OnLoadComplete
method.
PreRender:
This event is raised after the Page object has created all controls that are
required in order to render the page, including child controls of composite
controls. The Page object raises the PreRender event on the Page, and then
recursively does the same for each child control. Use the event to make final
changes to the contents of the page or its controls before the rendering stage
begins.
In this
event,
Page
ensures that
all child controls are created. Page
calls EnsureChildControls
for all controls, including itself. Every control whose
datasource/databind property is set calls for its databind method.
Order:
5. Content
page PreRender event.
6. Master
page PreRender event.
7. Master
page controls PreRender event.
8. Content
page controls PreRender event.
PreRenderComplete: This event is raised after each
control's
PreRender
property
is completed. This event ensures
the completion of the pre-rendering phase.
SaveStateComplete: This event is raised after view state and
control state have been saved for the page and for all controls. Any changes to
the page or controls at this point affect rendering, but the changes will not
be persist on the next postback.
Render: Every ASP.NET control has render
method and the page instance calls this method to output the control’s markup,
after this event any changes to the page or controls are ignored.
If you create a custom control, you typically
override this method to output the control's markup. However, if your custom
control incorporates only standard ASP.NET Web server controls and no custom
markup, you do not need to override the Render method.
A user control automatically incorporates
rendering, so you do not need to explicitly render the control in code.
Unload:
This event is raised for each control and then for the page. the UnLoad phase
is the last phase of the page life cycle. During the unload stage, the page and
its controls have been rendered, so you cannot make further changes to the
response stream. If you attempt to call a method such as the Response.Write
method, the page will throw an exception.
In controls, use this event to do final
cleanup for specific controls, such as closing control-specific database
connections.
For the page itself, use this event to do
final cleanup work, such as closing open files and database connections, or
finishing up logging or other request-specific tasks.
The
following is the sequence in which events occur when a master page is merged
with a content page:
1. Content
page PreInit event.
2. Master
page controls Init event.
3. Content
controls Init event.
4. Master
page Init event.
5. Content
page Init event.
6. Content
page PreLoad event
7. Content
page Load event.
8. Master
page Load event.
9. Master
page controls Load event.
10. Content
page controls Load event.
11. Content
Page LoadComplete event.
12. Content
page PreRender event.
13. Master
page PreRender event.
14. Master
page controls PreRender event.
15. Content
page controls PreRender event.
16. Master
page controls Unload event.
17. Content
page controls Unload event.
18. Master
page Unload event.
19. Content
page Unload event.