The Basics of Creating Server Controls
page 3 of 7
by Justin Lovell
Feedback
Average Rating: 
Views (Total / Last 10 Days): 51858/ 93

The Life Cycle of a Page and its Controls

In classic ASP, there was only one stage to the life cycle of the execution in the page. Basically, the page's life would start at the top of a script file (after IIS got all the server include files) and end at the bottom of the script file.

Because .NET is object orientated, there is a thing called events. With the aid of these events, the page/control life cycle is a bit more "eventful". Here is the basic order of events (without post back):

  1. Initialize (in your code, it is called the Init event) – Initialize settings needed during the lifetime of the incoming web request. If the server control is a composite control, this will load the default items based in the CreateChildControls method.

  2. Load – Perform actions common to all requests. This is the usual event that the page developer does his/her processing.

  3. PreRender – Perform any updates before the output is rendered. This is the usual event that control developers does his/her processing. This event is the last one that can do the last major modifications by the developer.

  4. Save View State - The ViewState property of a control is automatically persisted to a string object after this stage. Normally, this is stored to a hidden field on the page but can be persisted to another medium if need be.

  5. Render – Generates output to be rendered to the client. Note that not much functionality is able to be done here.

  6. Dispose – Perform any final clean-up before the control is torn down. Database connections, files and such should be closed at this point.

  7. Unload – Perform any final clean-up before the control is torn down.

This is the life cycle or order of events that the page undergoes during post back (note that some of these events do not have a description due to the fact that it was described in the above listing):

  1. Initialize – After the normal initializing is done, as described above, it will then do the following:

    1. Load View State – Loads the ViewState from the persistent medium. It then searches for the controls to load its ViewState.

    2. Process Post Back Data – Processes incoming data. This will only fire on controls that implement the IPostBackDataHandler interface.

  2. Load  – After the normal Load event is done, as described above, it will then do the following.

    1. Sends post back notifications – Raise change events in response to state changes between the current and previous postbacks. This will only fire on controls that implement the IPostBackDataHandler interface.

    2. Handle Post Events – Raises the event that the client-side triggered the post back for. First, it searches for the control that the event was targeted for. Once found, it determines if the control implements IPostBackEventHandler interface. If the interface is implemented, the RaisePostBackEvent method is called with a string argument which will then determine what event to raise.

  3. PreRender

  4. Save View State

  5. Render

  6. Dispose

  7. Unload

There is some behaviour that some of us is not consciously aware of but we use it to our advantage. The one behaviour is the first control to be alive will be the Page. Then its children starts to live, and their children starts to live and so on down until all the descendants are alive. The last control(s) that become alive is the first control(s) to die; therefore, we can conclude the page is the last one to die. To exagurate on this fact, we will then talk in lame-man's terms:

When the page begins its life, it would start with the Init event. Once the page has lived through the Init event, it will call the child(rens) Init event. Once those childrens' Init event has fired, their child(rens) starts to live through Init event and so it carries on down the hierarchy chain; for every, individual event as well. Therefore, the page starts life earlier (the parent control will live first).

I mentioned that the first control to live is the last to die. The reason is that all the controls in the page will execute the Unload event. When the Unload event is finished on the control that is located the furthest down the hierarchy, the control is finished. Its parent will then do the final finishing ups and then will finish its self. This bubble effect will continue until the Page gets the go ahead to do the finishing touches - therefore, the page is the last to die (the last control to "die").

Although many of us do not notice the following described behaviour now, whenever a control is dynamically added to a control collection, it will be given life... a rather speedy one! The control that was dynamically added plays catch up with the life cycle to the current point of the parent's point in its life cycle. For example, if the parent control is in its PreRender event and a control is dynamically added to the control collection of this parent, the child control will catch up to, and including the PreRender event.


View Entire Article

User Comments

Title: How do I put this control on a page?   
Name: G Shields
Date: 2010-07-21 11:53:01 PM
Comment:
Okay, so I have the inherited custom control class. How do I put it in my page?
Title: Need a translator   
Name: Rob
Date: 2009-05-18 2:24:18 PM
Comment:
Overall I found this article helpful, in some places though the English is so bad that it is practically illegible. As a result, I may have missed some of the finer points.
Title: Simply The Great   
Name: Dot Net Lover
Date: 2008-12-18 10:26:30 PM
Comment:
An Excellent Article.
Thanx for writing.
10 out of 10.
Title: Spell check please   
Name: bszom
Date: 2007-05-23 4:33:57 AM
Comment:
Lame-man's terms? Please, this article would be 3x as good had it been spell-checked.
Title: BRILLIANT explanations and structure!   
Name: Magnus B
Date: 2007-01-26 11:56:21 AM
Comment:
Thank you for gathering what I need to know in such a professional and independent page. Just the way I would ;)
Title: Good work   
Name: Narendra S. Jakhotia
Date: 2006-11-07 4:50:52 AM
Comment:
this topic Cleared most of my concepts on creating controls. I need help from U . I want to combine properties of Image button and Label please tell me how can I do it?
If possible please help me out.naren_pict@yahoo.co.in
Title: Excellent Example   
Name: Jayesh Chudasama
Date: 2006-09-01 2:22:23 AM
Comment:
Great Example!! It outlines all basic needs to understand to create server control.
Title: The Basics of Creating Server Controls   
Name: Jan Waiz
Date: 2005-08-04 11:53:17 AM
Comment:
Perfect!
Title: your history on classic ASP   
Name: JP
Date: 2005-08-03 3:42:12 AM
Comment:
You history on Classic ASP is not quite right. Classic ASP supported use of functions and subroutines which allowed code to be placed anywhere in your page. It also supported use of code classes and window script components (WSC). WSC files are components in XML format which did not have to be registered on the server.

Product Spotlight
Product Spotlight 





Community Advice: ASP | SQL | XML | Regular Expressions | Windows


©Copyright 1998-2024 ASPAlliance.com  |  Page Processed at 2024-03-28 12:34:23 PM  AspAlliance Recent Articles RSS Feed
About ASPAlliance | Newsgroups | Advertise | Authors | Email Lists | Feedback | Link To Us | Privacy | Search