Here is I’m going to explain, behind the scenes when a ASP.NET page loads.
The following are the various stages a page goes through:
The request page is either parsed or compiled, or fetched from the cache.
Page properties like the Request and Response are set. The type of request is determined, specifically whether it is a new Request or it is a PostBack. Culturing properties are also determined via the pages ICulture property.
All the controls on the given page are initialized with a UniqueID (please don’t confused this with the ID property as the UnqiueID is a unique, hierarchicial identifier which includes the server control’s naming container). Theming is also applied at this stage.
If the current request is a PostBack, the data from the viewstate and control state is loaded to the appropriate properties of the controls.
The Validate method of all the validator controls are fired, which in turn sets the Boolean property IsValid of the controls.
Postback Event Handling
If the current request is a PostBack, all event handlers are called.
ViewState data is saved for all the controls that have enabled viewstate. The Render method for all the controls is fired which writes its output to the OutputStream via a text writer.
Once the page has been rendered and sent, the Page’s properties are unloaded (cleanup time).
So know you have a better understanding of the various stages of a ASP.NET pages life cycle, you should be aware that within each of the above stages there are events that you can hook into so that your code is fired at the exact time that you want it to.
Event Wire-up is another important concept to understand. So Event wire-up is where ASP.NET looks for methods that match a naming convention (e.g. Page_Load, Page_Init, etc) , and these methods are automatically fired at the appropriate event. There is a page level attribute AutoEventWireup that can be set to either true or false to enable this behaviour.
Below are some of the more popular events that you should understand as you will most likely be interested in them:
You can use this event to:
· Create /recreate dynamic controls
· Set a master page or theme dynamically
· Access profile properties
Used to read or initialize your control properties.
Used when you need to access your properties after they have been initialized.
Used when you need to process things before the Load event has been fired.
The Page’s OnLoad method and all of its child control’s OnLoad method are fired recursively. This event is used to set properties and make database connections
Control specific events are handled at this stage. e.g. Click event’s for the button control.
This stage is for when you need to access controls that have been properly loaded.
Can be used to make any ‘last chance’ changes to the controls before they are rendered.
This event is used for things that require view state to be saved, yet not making any changes to the controls themselves.
The page object calls this method on all of the controls, so all the controls are written and sent to the browser.
All the controls UnLoad method are fired, followed by the pages UnLoad event (bottom-up). This stage is used for closing database connections, closing open files, etc.It is import to understand that each server control has its very own life cycle, but they are fired recursively so things may not occur at the time you think they do (they occur in reverse order!). What this means is that some events fire from the bottom up like the Init event, while others load from the top-down like the Load event.