Explain ASP.NET Page life Cycle Events with example?

ASP.NET Page life cycle having different events, which are having a sequence of flow i.e. each event will fire one after one in an ordered manor. These events would be coded. The following are the various events of ASP.Net page life cycle-

  • PreInit 
  • Init 
  • Init Complete
  • PreLoad 
  • Load
  • Control Event (PostBack Event )
  • Load Complete
  • PreRender 
  • Save State Complete
  • Render 
  • Unload

PreInit ()

  • This is the first event which raised in asp.net page lifecycle
  • Check for Request is for Post Back or not.
  • Create or re-create dynamic controls.
  • Theme Change, Master Page Set at runtime
  • The IsCallback and IsCrossPagePostBack properties have also been set at this time.

Note: If the request is a postback then the values of the controls have not yet been restored from the view state. If you set a control property at this stage, its value might be overwritten in the next event.

protected void Page_PreInit(object sender, EventArgs e)
    {
    }

PreInit ()

  • This is the first event which raised in asp.net page lifecycle
  • Check for Request is for Post Back or not.
  • Create or re-create dynamic controls.
  • Theme Change, Master Page Set at runtime
  • The IsCallback and IsCrossPagePostBack properties have also been set at this time.

Note: If the request is a postback then the values of the controls have not yet been restored from the view state. If you set a control property at this stage, its value might be overwritten in the next event.

protected void Page_PreInit(object sender, EventArgs e)
    {
    }

Init ()

  • Raised after all controls have been initialized
  • Turn on view state monitoring – any changes in control will be tracked by View State for future.
  • Build up a tree of controls from the ASPX file. This event is fired first for the bottom-most control in the hierarchy.
  • Each control's UniqueID is set and any skin settings have been applied.
  • Use this event to read or initialize control properties.

protected void Page_Init(object sender, EventArgs e)
    {
    }

Init Complete ()

  • This event raised at the end of the page's initialization stage.
  • Use this event to make changes to view state that you want to make sure are persisted after the next postback.
  • Use this event for processing tasks that require all initialization to be complete.

protected void Page_InitComplete(object sender, EventArgs e)
    {
    }

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.
  • Load view state data for page and controls
  • Load Postback data if needed
  • We can process any kind operation that need to perform before page load


protected override void Preload(EventArgs e)
    {
    }

Load ()

  • This is the first place in the page lifecycle that all values are restored.
  • We can create the connection initialization for any kind of external source like database connection
  • We can also set the control properties
  • Most code checks the value of IsPostBack to avoid unnecessarily resetting state.
  • You may also call Validate and check the value of IsValid in this method.


protected void Page_Load(object sender, EventArgs e)
    {
    }

Control Event (PostBack Event )

  • In a postback request, Corresponding events will trigger such as a Button control's Click event or a TextBox control's TextChanged event.
  • Individual validation controls also trigger before performing any processing
  • This is just an example of a control event. Here it is the button click event that caused the postback-

protected void Button1_Click(object sender, EventArgs e)
    {
    }

Load Complete ()

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

protected void Page_LoadComplete(object sender, EventArgs e)
    {
    }

PreRender ()

  • Raised after the Page object has created all controls that are required in order to render the page, including child controls of composite controls.
  • EnsureChildControls is also being called during this event
  • DataBind method for all control has also been called
  • If we want to change anything to any control this is the last event where we can do because after the pageRender starts  
  • This event takes place before saving ViewState, so any changes made here are saved.For example: After this event, you cannot change any property of a button or change any viewstate value.

protected override void OnPreRender(EventArgs e)
    {
    }

Save State Complete ()

Raised after view state and control state have been saved for the page and for all controls.

View State data saved in hidden filed called _VIEWSTATE

Use this event perform tasks that require the view state to be saved, but that do not make any changes to controls.

protected override void OnSaveStateComplete(EventArgs e)
    {
    }

Render ()

  • Pages call the Render method for each and every control (and not an event).
  • 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.

UnLoad ()

  • This is the last event of asp.net page life cycle
  • This ensure the Request and Response has been set to null.
  • This is called only after the content of the page fully rendered and response sent to client
  • Closing opened files
  • Closing database connections

protected void Page_OnLoad(object sender, EventArgs e)
    {
    }

Need Help? Contact Us.

Log in

*
*

Forgot password?

*

New User

*
*
*
*