Friday 31 July 2015

ASP.net Page Life Cycle



I find many developers not giving due consideration to page life cycle and end up writing all of their code in page_load event. Do, consider this, it will improve performance of your web application.
Its just an overview, not in depth. Hope it helps :
  1. PreInit
  • Determines page is postback or a new page
  • Masterpages are set
  1. Init
    • Builds up a tree of control (Controls are initialized)
    • Process viewState. Controls might update its state. (ViewState data is keyed by Control ID). eg:Label1.Text = "Nice Post"
    • Any viewstate that didn't match respective control ID is stored in a unmatched list.
    • Turn on view state tracking - Any changes to controls will now be recorded in the View State.
    • When the form was submitted some controls (for example TextBox1) will submit their values as a {ID, value} pairs. We call this the Post Data.
    • As before, we match Post Data against control by ID, giving each control a chance to process the data. Some controls may decide to trigger an action based on this, so they get added to a "Pending Actions" list.
    • Finally any unmatched Post Data also gets added to an "Unmatched"
    • Also, style sheets and themes are applied here.
  2. Load
    • This is a chance for controls to access the database, etc.
    • Some dynamically created controls may be added to the tree at this stage.
  3. Events
    • We make a second pass of any data saved in our "Unmatched" list. This is for the benefit of dynamically created controls.
    • We process the "Pending Actions" list and raise appropriate events (things like TextBox1 "On Changed").
    • We find the control that caused the post back event and see if it has any events it needs to raise - (typically things like Button1 "On Click").
  4. Render
    • View state tracking is turned off & View State serialized into the hidden _VIEWSTATE field.
    • Controls generate HTML based on their current state.
  5. Unload
    • Performs clan-up work like closing opened files, database connections etc.

No comments:

Post a Comment