Introduction To ASP.NET Page Life Cycle
The following article provides an outline for ASP.NET Page Life Cycle. Whenever you request a web page, it just doesn’t appear directly on your screen. There is a series of process that lets you view the page. Upon request, it is loaded into the memory and after going through some set of stages like (initialization, instance creation, restores state, maintain state, run event handler and render) and then it reaches your browser window. Understanding the life cycle is important before you can write appropriate code. To check the controls one can set trace as “true” in the page directive.
Stages Of ASP.NET Page Life Cycle
Stages in ASP.NET Page Life Cycle includes initialization of the event, instance creation, restore & maintain the state, execute event handler codes and render method. These life cycle events let the developer write code for dynamic web apps which helps dynamic communication between browser & server. A developer can use C#, Visual Basic.Net, or JavaScript as a language.
Given below is the each stage of ASP.NET Page Life Cycle.
1. PreInit
- As the name suggests Pre of Init, is raised once the start page is complete and the initialization stage begins.
- It creates and recreates dynamic control.
- Dynamically sets page and theme property
2. Init
- Of course, after preInit, Init would come into the picture. It is raised once the controls are initialized.
- For each control, an Init event occurs before the Init event of the Page.
3. InitComplete
- It is raised at the end of the initialization stage of the page.
- It enables turning on the tracking of changes of view state.
- If the tracking is not turned on then any values added in the view state would be lost.
4. OnPreLoad
- It is raised once the page loads view state for self and all of its controls.
- OnPreLoad then processes the Postback data which comes with the Request Instance.
5. Load
- It calls the OnLoad method on page object, the same method is then recursively performed same for all the child controls.
- Load event for each control occurs once the Load event of the page has occurred.
6. PostBack
- PostBack is the process of submitting a page to the server.
- Check the IsValid property of each validation control, you can think of it as credential checking.
- IsValid property of the page (in case if it has the validator controls) is checked before performing any kind of process.
7. LoadComplete
- LoadComplete as the name suggests is raised once the event-handling stage has been completed.
- LoadComplete is used for the tasks where all controls on the page are loaded.
8. OnPreRender
- OnPreRender is raised once the object of the ASP.NET page has created all the controls (Child controls as well) that are required to render the ASP.NET page.
- The OnPreRender event of each control occurs once the OnPreRender for the ASP.NET page has occurred.
- The object of the page raises the OnPreRender event on the ASP.NET page object, and then recursively for each child control.
9. OnSaveStateComplete
- OnSaveStateComplete is raised once the view state and its control states are saved for the page and its associated controls.
- At this point, all the changes on this ASP.NET page will be ignored.
10. RenderMethod
- RenderMethod differs as it is not a method instead it is an event, an ASP.NET page calls RenderMethod on each of its controls.
- This RenderMethod is associated with each web server and writes control markups to be sent to the browser.
11. Unload
- Unload as the name suggests unloading or cleaning up. It is the last event in this life cycle.
- As it is the last event all the processing has been happened and now all the objects and controls can be disposed off.
- Unloading is performed for class instances, open files, database connections.
- Unloading occurs first for events and then for the ASP.NET page.
Conclusion
In this article, we have reviewed the whole life cycle of the ASP.NET page. We have seen once the web page is requested it goes through a definite process before it appears in front of the user.
Recommended Articles
This is a guide to ASP.NET Page Life Cycle. Here we discuss the whole life cycle of the ASP.NET page along with various stages of ASP.NET Page Life Cycle in detail. You can also go through our other suggested articles to learn more –
Are you preparing for the entrance exam ?
Join our Programming Languages test series to get more practice in your preparation
View More