Updated June 30, 2023
Introduction to ASP.NET Page Life Cycle
Whenever we request a page, firstly, it is loaded into the memory, then processed, and sent to the browser. After that, it gets unloaded from memory. In each step, methods, as well as events, are available that can be overridden depending upon the need of the application. Page class first creates a hierarchical tree of all the controls. All these components, except for the directives which are a part of this tree. We could see the control tree if we add trace= “true” to the page directive.
ASP.NET Page Life Cycle
The phases of the ASP.NET Page Life Cycle is Initialization, Instantiation of controls, Restoration and maintenance of state, Execution of event handler codes, and Page rendering. ASP.NET Page Life Cycle is basically a web application framework that was developed and marketed by Microsoft in order to allow programmers to build dynamic web applications. It permits users to use fully featured languages like C# or VB.NET . It uses the HTTP commands, works on top of the HTTP protocol, and uses HTTP policies in order to set bilateral communication for browser-to-server. ASP.NET codes can be written in any of these languages: C#, J#, JScript or Visual Basic.Net. ASP.NET has a large number of controls like text boxes, buttons, and labels that can be required for manipulating and assembling the code for the creation of HTML pages.
The various stages of the ASP.NET Page Life Cycle are as below:
PreInit
- In this stage, the IsPostBack property is checked so as to find whether or not it is the first time that the page is being processed.
- Creation or re-creation of dynamic controls.
- Setting up the master page dynamically.
- Setting up the Theme property dynamically.
Init
- This stage fires up after each control has been initialized.
- Every control’s UniqueID is set.
- This stage is also used to initialize control properties.
- The event is first of all fired for the bottom-most control and then fired upwards the hierarchy until the page itself.
InitComplete
- This event can be used to make changes to the view state.
- This event is raised by the Page object.
- The event can be used for task processing that needs initialization to be completed.
OnPreLoad
- This particular event is always raised after the state is displayed by page load for itself as well as all controls, as well as after it processes the postback data that has gone along with the Request instance.
- OnPreLoad also loads view state for itself as well as all controls before the Page instance raises this particular event, and after that processes any postback data in the Request instance.
Load
- The load method is called on the Page object, and then recursively, it does the same for each child control all controls are loaded. The Load event of the page occurs before the Load event of the individual controls.
- It is the first place in the page lifecycle where all values are restored.
- The value of IsPostBack is checked mostly so as to avoid unnecessary resetting state.
- We can call the validate method and verify that IsValid.
- We can also create dynamic controls here.
- OnLoad event method is also used to establish database connections and set properties in controls.
Control PostBack Event(s)
- NET would now call those events on the page or either its controls that had caused the PostBack to take place.
- In this event, if the page has any of the validator controls, we need to check the IsValid property of the Page before we perform any kind of processing, and also we need to check the same property for individual validation controls.
- It is an example of a control event such as the button click event that has caused the postback.
LoadComplete
- This event is raised at the end of the event-handling stage.
- LoadComplete event is used for the work that requires all remaining controls on the page to get loaded.
OnPreRender
- OnPreRender event will be raised only after the Page object has created all required controls as well as child controls in order to render the page.
- PreRender event on the Page object is raised by Page Object, and afterward, in a recursive manner, it does the same for each of the children.
- The PreRender event of the page occurs before the PreRender event of the individual controls.
- It also allows final changes to the page.
- This stage occurs before saving ViewState, so changes that are made here are saved.
OnSaveStateComplete
- This is raised after the view state as well as the control state have been saved for the page and controls.
- ViewState has been saved for the page and all controls before this particular event occurs.
- Any changes that are made to the page or controls at this stage would be ignored.
- The render method is the method of the page object and its controls, and it is not an event.
- This method generates the Dynamic Hypertext Markup Language (DHTML), client-side HTML, and also the scripts that are necessary to properly display control in the browser.
UnLoad
- This last event is basically used for cleaning up.
- Here, all of the processing has occurred, and it is now safe to dispose of any remaining objects that include Page object as well.
- Cleanup is performed on:
- Instances of classes that are objects.
- Closing of opened files.
- Closing of database connections.
- Unload event occurs for each control and then finally for the page.
- During this stage, the page and its controls have been rendered, thus not making it possible to make further changes to the response stream.
- If you call a method such as the Response. Then the page would throw an exception.
Conclusion
Therefore we can conclude that whenever a page is requested from the browser by the user, the request would go through a series of steps, and various things would happen in the background so as to produce the output and send the response back to the client. The duration between this request and the response of a page is known as the “Page Life Cycle”.
Recommended Articles
This has been a guide to ASP.NET Page Life Cycle. Here we discuss the Concept, Various Stages, and Phases of the ASP.NET Page Life Cycle. You can also go through our other suggested articles to learn more –