When a user send request to the Web server, the page passes a lot of events from Initialization to Unloading. Generally an Asp.net Aspx page contains a lot of server side controls, HTML controls as well as user controls. Most of the developers does not know the accurate life cycle events of an Aspx page. But its good to know because to be an efficient & knowledgeable developer or programmer you have to know the Life Cycle of an Asp.net Aspx page very well. Otherwise you will be failed to get advantage from it specially when developing user controls, handlers etc. It’s also a common & crucial or a vital question of Asp.net interview viva or written exam.
For better understanding I have divided the Sequential loading of a page cycle in 2 ways as follows:
1. First time request of a page
2. Postback of a page
See the Overview from below:
First time request of a page:
1. Object Initialization: Creates instance of the server control. The initialization event can be overridden using the OnInit method. The event associated with the cycle is Page_Init. In this phase the page knows the types of objects and how many to create.
3. PreRendering: Associated value of the control is assigned. This is the last time changes of objects to save into the viewstate. After the execution of the method, controls value is locked for the viewstate. The PreRender step can be overridden using OnPreRender method. The event associated with the cycle is Page_PreRender.
4. Saving: The state values of the control is saved to the viewstate. The value is attached in the HTML tag which we found in the browser view source action menu. It can be overridden by calling SaveViewState method.
5. Rendering: In this page corresponding HTML tag of the controls will be created. It can be overridden by calling OnPreRender method. The event associated with the cycle is Page_Render.
6. Disposing: At this stage the pages objects will be disposed. Basically this is the cleanup stage. Close all files, DB connections in this stage.
7. Unloading: This is the final event in the life cycle of the server control. In this phase all server control instances will be destroyed. The event associated with the cycle is Page_UnLoad.
Postback of a page:
1. Initializing: Same as before.
2. Loading View State: In this stage controls are populated with the appropriate viewstate data.
3. Loading: Same as before.
4. Loading the postback data: In this phase control states updated with the correct postback data.
5. PreRendering: Same as before.
6. Saving State: The change of control between the current request and the previous request of the page is saved. For each change, the corresponding event is raised. For example, if the text of a textbox is changed, the new text is saved and a text_change event is raised.
7. Rendering: Same as before.
8. Disposing: Same as before.
9. Unloading: Same as before.
Hope this preparation will help you a lot to face ASP.net interview viva.