Click here to Skip to main content
16,018,057 members
Articles / Web Development / ASP.NET
Article

The ASP.NET Page Lifecycle – A Basic Approach

Rate me:
Please Sign up or sign in to vote.
4.89/5 (100 votes)
28 Sep 2007CPOL6 min read 196.8K   239   24
A simple approach for understanding the ASP.NET page lifecycle.

Introduction

For ASP.NET developers, understanding the ASP.NET page lifecycle is important for many reasons, but primarily for knowing where to place particular methods, and when various page properties are set. However, it can often be difficult to remember and make sense out of all of the methods available in the page lifecycle. While there are scores of articles on the internet related to the internal mechanics of the page lifecycle, this article intends to begin by covering the basics and providing a simple and easy understanding of usage.

It can be difficult to remember exactly what happens and when during the ASP.NET page lifecycle. That in mind, often the easiest way to make sense out of a series of steps is to create an acronym. Microsoft shows us the basic steps of the ASP.NET page lifecycle below.

  1. Page Request
  2. Start
  3. Page Initialization
  4. Load
  5. Validation
  6. Postback event handling
  7. Rendering
  8. Unload

Putting together an acronym for these is easy enough. Since the Page Request technically isn't a part of the life cycle (it only indicates whether we actually will start the cycle or load a cached page) we won't include it in the acronym.

  • S – Start
  • I – Initialize
  • L – Load
  • V – Validate
  • E – Event Handling
  • R – Render

That gives us "SILVER", which is very easy to remember. However, it is important remember that the last part of the cycle is unload. You can remember it as "SILVER-U" or "SILVER-YOU" if that helps (but it just didn't quite fit into our acronym!). Now that it's easy to remember the order of steps for the page lifecycle, we'll summarize exactly what happens and what events are pertinent to each stage.

1. Start

This is where page properties such as Request, Response, IsPostBack and UICulture are set. As a developer, you most likely won't really need to do anything in this stage of the cycle. If you need to access or override behavior for this step, use the PreInit method to create or re-create dynamic controls, set a master page or theme or read or set profile property values. It is important to note that if the request is a postback, the values of the controls have not yet been restored from view state. If you set a control property at this stage, its value might be overwritten in the next event.

2. Initialize

This stage can be very important to developers. Here, themes are applied, and unique ids are generated and set for controls. Developers have access to the Init, InitComplete and PreLoad methods in this stage. Microsoft's recommended usage for these methods is as follows:

  • Init – This event is raised after all controls have been initialized and any skin settings have been applied. Use this event to read or initialize control properties.
  • InitComplete – This event is raised by the Page object. Use this event for processing tasks that require all initialization be complete.
  • PreLoad - Use this event if you need to perform processing on your page or control before the Load event. After the Page raises this event, it loads view state for itself and all controls, and then processes any postback data included with the Request instance.

3. Load

This stage is perhaps the most utilized by developers. In this stage controls are loaded with information retrieved from view and control states. The OnLoad is the event method that fires during this stage. This is where you will want to set properties for all of the server controls on your page, request query strings, and establish database connections.

4. Validation

If you have controls that require validation, they are validated here and you can now check the IsValid property of the control. The event associated with this is Validate, which contains one overloaded method that accepts a validation group string. The overloaded method instructs the controls in the specified group to validate.

5. Event Handling

The event handling for server controls occurs during this stage. This means that events such as Click, SelectedIndexChanged, etc are applied to your server controls, and, in the case of a postback, these event handlers are fired by the control. The accessible events of note in this stage are as follows:

  • LoadComplete – At this step, all of the controls for the page have been loaded.
  • PreRender – A few things of import happen here. First, the page object will call EnsureChildControls for each control, and finally for the page. Additionally, any data bound control that has a DataSourceID set will call its DataBind method. It is important to note that the PreRender event occurs for each control on the page. At the conclusion of this event, ViewState will be saved for the page and all of the controls.
  • SaveStateComplete – ViewState has been saved. If you have actions that do not require changes to controls but require ViewState to have been saved, you can handle the SaveStateComplete event.

6. Render

Render is not really an event. Rather, the page object calls this method on each control, which in turn writes out the HTML markup for the control to the browser. This stage is keenly important to developers who create custom controls, because the standard approach is to override the Render method for the control in order to output the custom markup. If your control inherits from a standard ASP.NET server control, you probably won't need to override the Render method unless you want to exhibit a different behavior than the control's default. This is outside the scope of this document, but for more reading, you can reference Microsoft's Developing Custom ASP.NET Server Controls. (http://msdn2.microsoft.com/en-us/library/zt27tfhy.aspx)

7. Unload

This final event occurs first for each control, then, finally, for the page. At this point, all controls have been rendered to the output stream and cannot be changed. During this event any attempt to access the response stream will result in an exception being thrown. This event is primarily for cleanup routines such as closing open database connections and open file streams, or, event logging and other tasks.

Methods

The following methods (which can all be overridden) occur in order during the lifecycle of an ASP.NET page. Please realize that some of these methods are called recursively, and multiple times depending on the content of the page. This list is the generalized order in which methods fire when a page loads. You can test this by creating a default ASP.NET application, overloading each of the below methods, and setting a breakpoint on each.

  1. Construct
  2. ProcessRequest
  3. FrameworkInitialize
  4. InitializeCulture
  5. If child controls are present:
    1. AddParsedSubObject
    2. CreateControlCollection
    3. AddedControl
    4. ResolveAdapter
  6. DeterminePostBackMode
  7. OnPreInit
  8. OnInit
  9. TrackViewState
  10. OnInitComplete
  11. OnPreLoad
  12. OnLoad
  13. OnLoadComplete
  14. EnsureChildControls
    1. CreateChildControls
  15. OnPreRender
  16. OnPreRenderComplete
  17. SaveViewState
  18. OnSaveStateComplete
  19. CreateHtmlTextWriter
  20. RenderControl
  21. Render
    1. RenderChildren
    2. VerifyRenderingInServerForm
  22. OnUnload
  23. Dispose

Conclusion

When developing ASP.NET applications, it's important to know what happens when. Understanding how events unfold inside of the page will save you several hours of headache and debugging. While the order of the methods may be hard to remember, I hope that applying the anagram will be of considerable use when determining what needs to happen where inside of your application.

I put this article together as much to help other people, as to help myself. Even experienced developers can sometimes forget the order in which things occur. This is not intended to be an all-encompassing article. Rather, it is my hope that beginning and intermediate developers can carry this "hip pocket" material around to help mitigate at least some of the more common mistakes.

Happy ASP.NET'ing!

License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)


Written By
Architect Swivel
United States United States
I’m a very hands-on, full-stack, architect and engineer with management and leadership experience. I have operated primarily in the .NET environment, though I embrace Open Source wholeheartedly. I absolutely love writing code, developing standards and patterns, working with talented engineers, and mentoring individuals; I thrive in non-managerial environments. I’m passionate about the technology, science, mathematics, and engineering behind the craft of computer science. Even when I’m not working my full-time job, I’m still honing my technical skills, whether it’s building rockets on my 3D printer, designing software for personal use, writing white papers, or contributing to my many open-source projects.

Comments and Discussions

 
QuestionVery Nice Article Pin
Avadhesh Kumar Maurya3-Jun-14 18:14
Avadhesh Kumar Maurya3-Jun-14 18:14 
GeneralMy vote of 2 Pin
Ashad2520-Jul-13 7:55
Ashad2520-Jul-13 7:55 
GeneralMy vote of 3 Pin
sandeepkumarvemula13-Feb-13 20:18
sandeepkumarvemula13-Feb-13 20:18 
GeneralMy vote of 5 Pin
BlehBlahBah16-Dec-12 20:17
BlehBlahBah16-Dec-12 20:17 
GeneralMy vote of 1 Pin
sawant parag7-Jun-12 19:11
sawant parag7-Jun-12 19:11 
GeneralMy vote of 5 Pin
Ragunathan Maniraj8-Mar-12 20:24
Ragunathan Maniraj8-Mar-12 20:24 
GeneralExcellent article Pin
thatraja19-Jan-10 2:57
professionalthatraja19-Jan-10 2:57 
Generalwhere does control events happens Pin
Arash Javadi26-May-09 21:49
Arash Javadi26-May-09 21:49 
GeneralMinor gripe Pin
dolear15-Nov-07 4:37
dolear15-Nov-07 4:37 
General5 for SILVER :) Pin
Senthil Kumar U12-Nov-07 21:27
Senthil Kumar U12-Nov-07 21:27 
Generalhey SILVER Pin
rameshisin21-Oct-07 22:37
rameshisin21-Oct-07 22:37 
GeneralExcellent Article Pin
M_Menon10-Oct-07 19:53
M_Menon10-Oct-07 19:53 
GeneralGreat Memory Tool Pin
charleslbryant3-Oct-07 18:04
charleslbryant3-Oct-07 18:04 
GeneralExcelent - a 5 from me too! Pin
Andrei Ion Rînea3-Oct-07 3:10
Andrei Ion Rînea3-Oct-07 3:10 
GeneralExcellent work - really clear - thanks Pin
Brian Lowe1-Oct-07 6:36
Brian Lowe1-Oct-07 6:36 
GeneralRe: Excellent work - really clear - thanks Pin
DreamInHex1-Oct-07 9:20
DreamInHex1-Oct-07 9:20 
GeneralRe: Excellent work - really clear - thanks Pin
Brian Lowe2-Oct-07 8:18
Brian Lowe2-Oct-07 8:18 
GeneralGood work Pin
yesdnn.com29-Sep-07 15:57
yesdnn.com29-Sep-07 15:57 
GeneralRe: Good work Pin
DreamInHex1-Oct-07 3:52
DreamInHex1-Oct-07 3:52 
GeneralRe: Good work Pin
dev058311-Oct-07 20:20
dev058311-Oct-07 20:20 
Questionwhat a surprise! Pin
Huisheng Chen29-Sep-07 2:59
Huisheng Chen29-Sep-07 2:59 
AnswerRe: what a surprise! Pin
DreamInHex1-Oct-07 3:51
DreamInHex1-Oct-07 3:51 
Generalaha, SILVER! Pin
Huyong Zhao28-Sep-07 17:43
Huyong Zhao28-Sep-07 17:43 
GeneralRe: aha, SILVER! Pin
DreamInHex28-Sep-07 18:15
DreamInHex28-Sep-07 18:15 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.