|
||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Announcements
Chapters
Services
Feature Zones
|
IntroductionFor 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.
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.
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. StartThis is where page properties such as 2. InitializeThis 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
3. LoadThis stage is perhaps the most utilized by developers. In this stage controls are loaded with information retrieved from view and control states. The 4. ValidationIf 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 5. Event HandlingThe event handling for server controls occurs during this stage. This means that events such as
6. RenderRender 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 7. UnloadThis 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. MethodsThe 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.
ConclusionWhen 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!
|
|||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||