Page Life Cycle – ASP.NET

Lifecycle1

Introduction

When an ASP.NET page runs, the page goes through a life cycle in which it performs a series of processing steps. These include initialization, instantiating controls, restoring and maintaining state, running event handler code, and rendering. It is important for you to understand the page life cycle so that you can write code at the appropriate life-cycle stage for the effect you intend.

Table of Contents

PreInit

#region PreInit
protected void Page_PreInit(object sender, EventArgs e)
{
  /***
   * PreInit is the first phase in asp.net page life cycle events
   * Check the IsPostback Property to determine whether this page is the first time the page is being processed.
   * The IsCallback and IsCrosspagepostback properties have also been set at this time.
   * Create or re-create dynamic controls
   * Set the Master page Dynamically
   * Set the theame property Dynamically
   * Read or Set Profile Properties Values.
   * ****/
}
#endregion

Init

    #region Init
    protected void Page_Init(object sender, EventArgs e)
    {
        /**
         * Read or Initialize Control Properties.
         * Build Control Tree for ASPX File.
         * Turn on view state monitoring-any changes in control will be tracked by view state for future. 
         * **/        
    }
    #endregion

InitComplete

    #region InitComplete
    protected void Page_InitComplete(object sender, EventArgs e)
    {
        /***
         * Viewstate functionality is turned on for server control.
         * Viewstate of controls allows preserving their values across postback 
         * **/
    }
    #endregion

PreLoad

    #region PreLoad
    protected void Page_PreLoad(object sender, EventArgs e)
    {
        /**
         * Loads ViewState : ViewState data are loaded to controls.
         * Loads Postback data Postback data are now handed to the page controls by IsPostBackDataHandler.
         * ***/
    }
    #endregion

Load

    #region Load
    protected void Page_Load(object sender, EventArgs e)
    {
        /***
         * The Load event Set Properties in Controls and to establish database Connections.
         * The page has been restored to its previous state in case of postbacks
         * this is the first place in the page lifecycle that all values are restored
         * You can also create dynamic controls in this method.
         * **/
    }
    #endregion

Control Events

    #region Controll Event
    protected void btnsubmit_Click(object sender, EventArgs e)
    {
        /**
         * In Events you can also check the Page.
           IsValid property if you’ve used any validator controls in your page like regularexpressionValidator,
           requiredFieldValidator etc. 
         * **/
    }
    #endregion

Load Complete

    #region LoadComplete
    protected void Page_LoadComplete(object sender, EventArgs e)
    {
        /**
         *  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
         **/
    }
    #endregion

PreRender

    #region PreRender
    protected void Page_PreRender(object sender, EventArgs e)
    {
        /**
         * Allows final changes to the page or its control.
         * This event takes place before saving ViewState, so any changes made here are saved.
         * DataBind method for all control has also been called.
         * Ensure the Child Controls is also being called during this events.
         * **/
    }
    #endregion

PreRenderComplete

    #region PreRenderComplete
    protected void Page_PreRenderComplete(object sender, EventArgs e)
    {
        /**
         * Raised after each data bound controls whose datasource id property is set calls its data bind method.
         * PreRenderComplete is the last place to change anything in the page.After this event any changes 
           will not get preserved in page cycle. 
         * **/
    }
    #endregion

OnSaveStateComplete

   #region SaveStateComplete
   protected override void OnSaveStateComplete(EventArgs e)
   {
       /**
         * Prior to this event the view state for the page and its controls is set. 
         * Any changes to the page’s controls at this point or beyond are ignored.
         * **/
    }
    #endregion

Render

   #region Render
   protected override void Render(HtmlTextWriter writer)
   {
       /**
        * 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
        * **/
       base.Render(writer);
   }
   #endregion

Unload

    #region Unload
    protected void Page_Unload(object sender, EventArgs e)
    {
        /**
         * This event is used for cleanup code
         * Instance of class and object
         * Closing Opened File
         * Close Database Connections 
         * **/
    }
    #endregion

References

Leave a Reply

Your email address will not be published. Required fields are marked *