Altaf Khatri
Altaf Khatri

RSS feed for website. Twitter account of website.
∗Recently Added - How to take care of Betta fighter fish and increase life span?  10/30/2013

Find me on web




ASP.Net Page Life Cycle And Events

Starting from the request to the response back of the ASP.NET page, from the browser to the ASP.NET hosted server and back to the browser constitutes the ASP.NET page life cycle.

In this article, the main ASP.NET page life cycle events are listed sequentially.

Copy the below code in the Default.aspx file and put break points on each method to understand the sequence of events.

It's a very nice refresher before any interview.

using System;

using System.Collections.Generic;

using System.Linq;

using System.Web;

using System.Web.UI;

using System.Web.UI.WebControls;


public partial class _Default : System.Web.UI.Page


    //  Event 1

    protected void Page_PreInit(object sender, EventArgs e)


        //  Use this event for the following:

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

        //  Note:

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





    //  Event 2

    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.




   //  Event 3

   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.     



    //  Event 4:Load view state for page


    //  Event 5

    protected override void OnPreLoad(EventArgs e)


        // Use this event if you need to perform processing on your page or control before the  Load event.

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




    //  Event 6

    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.



    //  Event 7

    // Control Events takes place after Page Load



    //  Event 8

    protected void Page_LoadComplete(object sender, EventArgs e)


        // Use this event for tasks that require that all other controls on the page be loaded.



    //  Event 9

    //Databind events takes place here


    //  Event 10

    protected override void OnPreRender(EventArgs e)


       // Before this event occurs:

        // The Page object calls EnsureChildControls for each control and for the page.

        // Each data bound control whose DataSourceID property is set calls its DataBind method. For more information, see Data Binding Events for Data-Bound Controls later in this topic.

        // The PreRender event occurs for each control on the page. Use the event to make final changes to the contents of the page or its controls.




   //  Event 11

    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.




    // Render stage goes here. This is not an event



    //  Event 12

    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.

        // For the page itself, use this event to do final cleanup work, such as closing open files and database connections, or finishing up logging or other request-specific tasks.

        // Note:

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



For more information on the ASP.NET page life cycle and events, please refer: ASP.NET Page life cycle and events

SiteMap  |  Contact Me