November 01, 2008

ASP.NET Page Life Cycle Demonstration


Let us take an example ASP.NET page with the following characteristics:


  • It contains a single Label control (lbl) with its “Text” property set to “statictext”
  • It contains a Button control (btnA) with code in its event handler that sets the “Text” property of “lbl” to “dynamictext”
  • It contains a Button control (btnB) whose purpose is to cause a page postback

Now, let us examine what will happen during the page life cycle.

  1. The page is first loaded

    1. The compiled class of the page is generated and “statictext” is assigned to “lbl.Text”
    2. Tracking for Viewstate is enabled in the “InitComplete” event
    3. “LoadViewState” is not fired because there is no postback
    4. “SaveViewstate” is fired, but nothing happens because no change of state is recorded
    5. The page is rendered with the value “statictext” inside “lbl”

  2. btnA is clicked

    1. The previously generated compiled class is handed the request; “lbl.Text” is set to “statictext”
    2. Tracking for Viewstate is enabled
    3. “LoadViewState” is fired, but nothing happens because no change of state was recorded in the previous page visit
    4. “RaisePostbackEvent” event is executed and “btnA” click event handler is fired; “lbl.Text” is set to “dynamictext”; since tracking is enabled at this stage, this item is tracked (marked as “Dirty”).
    5. “SaveViewState” is fired; “dynamictext” is serialized and stored in the __VIEWSTATE field
    6. The page is rendered with the value “dynamictext” inside “lbl”

  3. btnB is clicked

    1. The previously generated compiled class is handed the request; “lbl.Text” is set to “statictext”
    2. Tracking for Viewstate is enabled
    3. “LoadViewState” is fired; since a change of state was recorded from the previous page visit, __VIEWSTATE is loaded and the value “dynamictext” is extracted; this value is then assigned to “lbl”
    4. “RaisePostbackEvent” event is executed; nothing happens here because “btnB” has no event handler for its “Click” event
    5. “SaveViewState” is fired, but nothing happens because no change of state is recorded
    6. The page is rendered with the value “dynamictext” inside “lbl”

0 comments: