I am writing this blog because one of my friends asked me to write on this very important phase of asp.net programming. This is very hot question asked during interview.
Here I am explaining all the phases of ASP.NET page life cycle step by step and I also tried to show what happen in every phase of page life cycle.
| Page_Init | Page Initialization |
| LoadViewState | View State Loading |
| LoadPostData | Postback Data Processing |
| Page_Load | Page Loading |
| RaisePostDataChangedEvent | PostBack Change Notification |
| RaisePostBackEvent | PostBack Event Handling |
| Page_PreRender | Page Pre Rendering Phase |
| SaveViewState | View State Saving |
| Page_Render | Page Rendering |
| Page_Unload | Page Unloading |
Now see what happen in real life
- using System;
- using System.Collections.Generic;
- using System.Linq;
- using System.Web;
- using System.Web.UI;
- using System.Web.UI.WebControls;
- namespace WebApplication1
- {
- public partial class _Default: Page
- {
- protected void Page_PreInit(object sender, EventArgs e) {
- //Check the IsPostBack property to determine whether this is the first time the page is being processed.
- //Create or re-create dynamic controls.
- //Set a master page dynamically.
- //Set the Theme property dynamically.
- //Read or set profile property values.
- }
- protected void Page_Init(object sender, EventArgs e) {
- //Raised after all controls have been initialized and any skin settings have been applied.
- //Use this event to read or initialize control properties.
- }
- protected void Page_InitComplete(object sender, EventArgs e) {
- //Raised by the Page object. Use this event for processing tasks that require all initialization be complete.
- }
- protected void Page_Preload(object sender, EventArgs e) {
- //Use this event if you need to perform processing on your page or control before the Load event.
- /*After the Page raises this event, it loads view state for itself and all controls,
- and then processes any postback data included with the Request instance. */
- }
- protected void Page_Load(object sender, EventArgs e) {
- /* The Page calls the OnLoad event method on the Page,
- then recursively does the same for each child control,
- which does the same for each of its child controls
- until the page and all controls are loaded. */
- //Use the OnLoad event method to set properties in controls and establish database connections.
- }
- protected void Page_LoadComplete(object sender, EventArgs e) {
- //Use this event for tasks that require that all other controls on the page be loaded.
- }
- protected void Page_PreRender(object sender, EventArgs e) {
- /* 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 object,
- and then recursively does the same for each child control.
- The PreRender event of individual controls occurs after the PreRender event of the page.
- The PreRender event of individual controls occurs after the PreRender event of the page.
- Allows final changes to the page or its control.
- This event takes place before saving ViewState, so any changes made here are saved.
- */
- }
- protected override void OnSaveStateComplete(EventArgs e) {
- /* Before this event occurs, ViewState has been saved for the page and for all controls.
- Any changes to the page or controls at this point will be ignored.
- Use this event perform tasks that require view state to be saved,
- but that do not make any changes to controls. */
- }
- /* Now Here is Render: This is a method of the page object and its controls (and not an event).
- At this point, ASP.NET calls this method on each of the page’s controls to get its output.
- The Render method generates the client-side HTML, Dynamic Hypertext Markup Language (DHTML),
- and script that are necessary to properly display a control at the browser. */
- protected void Page_UnLoad(object sender, EventArgs e) {
- /* This event occurs for each control and then for the page.
- In controls, use this event to do final cleanup for specific controls,
- such as closing control-specific database connections.
- 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. */
- }
- }
- }

SubashPosted Aug 22, 2016, 10:37 PM
Good one
Santhakumar MunuswamyPosted May 16, 2015, 3:59 AM
Nice