Join the social network of Tech Nerds, increase skill rank, get work, manage projects...
 
  • What is the life cycle of a ASP.Net page and control?

    • 0
    • 2
    • 0
    • 0
    • 0
    • 0
    • 0
    • 0
    • 347
    Comment on it

    Hello coder's world,

    This article is regarding ASP.Net page and control's life cycle. I would like to start this with few basic questions, which should come in everyone's mind who wants to know about this.

    1. Why we need to know about ASP.Net page and control's life cycle?
    2. What is the ASP.Net page and control's life cycle?
    3. How we will use it?

    These three question are very basic to everything in .Net world. If you know the answer of these three questions, no one can defeat you in any field(sorry for the lecture but again it's true).

    So lets try to answer one by one:


    Why we need to know about ASP.Net page and control's life cycle?

    Ans 1: We need to know about page and control life cycle in following occasions :
    1. Create custom controls.
    2. Add control dynamically in a page.
    3. Run time binding of events.
    4. Create http module and http handlers.
    5. Request routing.
    6. Authorization and authentication.

    What is the ASP.Net page and control's life cycle?

    Ans 2: When a browser requests for a ASP.Net Page to IIS(Internet Information Server). Then IIS reads the request made by Browser/(other means) and look for the requested page. It runs the page server side, 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. Additionally, if you develop custom controls, you must be familiar with the page life cycle in order to correctly initialize controls, populate control properties with view-state data, and run any control behavior code. (The life cycle of a control is based on the page life cycle, but the page raises more events for a control than are available for an ASP.NET page alone.)

    Note:(To remember the ASP.Net page life cycle one must remember the word 'SILVER - U'.)


    Following are the general stages of a page life cycle:

    1. Start(This phase is confirmation of request response set. And check for the new post back.)
    2. Initialization(Page initialization, all controls on the page are available.)
    3. Load (all control's properties are loaded, getting information from view state)
    4. Validation(all validation controls are called and page Isvalid property set to true)
    5. Event(If the request is a postback, any event handlers are called.)
    6. Rendering(during this phase page call Render method for each control, providing a text writer that writes its output to the OutputStream of the page's response property.


    Following are the stages of a control's life cycle:


    Stage Description

    Initialize

    Initialize settings needed during the lifetime of the incoming Web request

    Load view state

    At the end of this phase, the ViewState property of a control is automatically populated as described in Maintaining State in a Control. A control can override the default implementation of the LoadViewState method to customize state restoration.

    Process postback data

    Process incoming form data and update properties accordingly

    Load

    Perform actions common to all requests, such as setting up a database query. At this point, server controls in the tree are created and initialized, the state is restored, and form controls reflect client-side data.

    Send postback change notifications

    Raise change events in response to state changes between the current and previous postbacks.

    Handle postback events

    Handle the client-side event that caused the postback and raise appropriate events on the server.

    Prerender

    Perform any updates before the output is rendered. Any changes made to the state of the control in the prerender phase can be saved, while changes made in the rendering phase are lost.

    Save state

    Save state The ViewState property of a control is automatically persisted to a string object after this stage. This string object is sent to the client and back as a hidden variable. For improving efficiency, a control can override the SaveViewState method to modify the ViewState property.

    Render

    Generate output to be rendered to the client.

    Dispose

    Perform any final cleanup before the control is torn down. References to expensive resources such as database connections must be released in this phase.

    Unload

    Perform any final clean up before the control is torn down. Control authors generally perform cleanup in Dispose and do not handle this event.

    How we will use it?


    Ans 3: To use these events are very simple. You can just go to .aspx.cs file write the following code:

     
     protected void Calendar_Init(object sender, EventArgs e)
      {
        // Do any related intialization work.
      }
      protected void Page_Load(){
       //does stuff
      }
    

    References


    To read more in depth go through following links:
    Asp.Net page life cycle
    Asp.net control's life cycle
    Happy Coding :)

 0 Comment(s)

Sign In
                           OR                           
                           OR                           
Register

Sign up using

                           OR                           
Forgot Password
Fill out the form below and instructions to reset your password will be emailed to you:
Reset Password
Fill out the form below and reset your password: