Sunday, October 9, 2016

ASP.NET Page Life Cycle

  • Asp.net programs is run in specific life cycle.It follows the step-by-step procedure.
  • Asp.net page life cycle gives the full details of program execution it will help in writing the program.
  • Whenever we are going to process customised control then that time you need to know the all the details like where we need to initialise the custom control and where you need to create the instance for custom control.

ASP.NET Page Life Cycle Phases/Stages :

  • ASP.NET page life cycle phases are following :
    • Page Request.
    • Start.
    • Initialisation.
    • Load.
    • Postback Event Handling.
    • Rendering.
    • Unload.

Page Request :

  • Page request is done before the page requesting.
  • While Page requesting only the asp.net decides the page needs to be parsed and compiled or cached version of the page sent as response with out running the page.

Start :

  • Start page will decide the request and response.
  • It also decides the page request is new request or postback request(old request) and then sets the 'IsPostBack' property.

Initialisation :

  • In Initialisation phase, all properties and unique id are set to the controls.
  • Master page and themes also applicable to the  page.

Load :

  • During the load, If current request is postback then, all properties are loaded with the information recovered from the viewstate and control state.

Postback Event Handling :

  • Postback is raised whenever the control event handlers are raised.
  • After postback, Validating the controls will happen if any control have the validation.

Rendering :

  • Before rendering the page,asp engine will process and result will generate.That generated result format will not the understand by the browser/client.
  • So, we need to render the page to the user(browser) understandable format.

Unload :

  • After sending the result to client, asp engine will unload processed page from the memory.

ASP.NET Page Life Cycle Events :

  • Asp.net page life cycle,each and every phase contains the events.Those events will used to handle the code.For control the events ,we will bind the event handlers with the relevant event, that will help to run the code.
  • Following are the page life cycle events :
    • PreInit.
    • Init.
    • InitComplete.
    • Preload.
    • Load.
    • LoadComplete.
    • PreRender.
    • Render.
    • RenderComplete.
    • SaveStatecomplete.
    • Unload.

PreInit :

  • PreInit raised after the start phase complete only.
  • In preInit , we will check the details like the page request is new request or postback request.
  • In this event we will create the dynamic controls and dynamically apply themes to the page.

  Init :

  • This event is used to read or initialising the properties.
  • Raised after loading the all the properties if the controls are loaded.
    

InitComplete :

  • Only one function will raise between the Init and InitComplete that is enabling the viewstate.

PreLoad :

  • Raised after the page loads view state for itself and all controls, and after it processes postback data that is included with the Request instance.
  • Before the Page instance raises this event, it loads view state for itself and all controls, and then processes any postback data included with the Request instance.
  • Loads ViewState: ViewState data are loaded to controls.
  • Loads Postback data: Postback data are now handed to the page controls.

Load :

  • the page has been restored to its previous state in case of postbacks.
  • Code inside the page load event typically checks for PostBack and then sets control properties appropriately. 

LoadComplete:

  • 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.

PreRender :

  • PreRender will raise after loading the object and that object is required to render the page.

Render :

  • This is not an event; instead, at this stage of processing, the Page object calls this method on each control. All ASP.NET Web server controls have a Render method that writes out the control's markup to send to the browser.

SaveStateComplete :

  • 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 retrieved on the next postback.

UnLoad :

  • Unload event will raise for each and every event.
  • The page itself raise this event to cleanup the files from the server memory.for example, closing the opened files.