The Page Life Cycle Events

No.of Views631
Bookmarked0 times
Downloads 
Votes0
By  amalhashim   On  15 Feb 2010 23:02:10
Tag : ASP.NET , How to
The Page Life Cycle Events
emailbookmarkadd commentsprint

Images in this article missing? We recently lost them in a site migration. We're working to restore these as you read this. Should you need an image in an emergency, please contact us at info@codegain.com

 

Introduction

As ASP.NET processes a page through its stages, various events are raised for the page and its related controls. You write code to handle these events and thus respond to various actions related to the processing of a page. For example, you might wish to write code that gets called when a page is first loaded to determine if the user is requesting the page or posting back to the server. This is a common scenario. When a page is first requested you often have to initialize data and controls. However, when it posts back you do not wish to run this code. Another common example is when a user clicks a button. You want to respond to the click event for that button only. Knowing which events are called in what order is important to making sure your code executes properly and can be debugged. Table 2-1 contains an ordered list of the more common events that are triggered when a page is processed by ASP.NET.

EventDescription
PreInit

This is the first real event you might handle for a page. You typically use this event only if you need to dynamically (from code) set values such as master page or theme.

This event is also useful when you are working with dynamically created controls for a page. You want to create the controls inside this event.

Init

This event fires after each control has been initialized. You can use
this event to change initialization values for controls.

InitComplete

Raised once all initializations of the page and its controls have been completed.

PreLoadThis event fires before view state has been loaded for the page and its controls and before PostBack processing. This event is useful when you need to write code after the page is initialized but before the view state has been wired back up to the controls.
Load

The page is stable at this time; it has been initialized and its state has been reconstructed. Code inside the page load event typically checks for PostBack and then sets control properties appropriately.

The page’s load event is called first. Then, the load event for each child control is called in turn (and their child controls, if any). This is important to know if you are writing your own user or custom controls.

Control (PostBack) event(s)ASP.NET now calls any events on the page or its controls that caused the PostBack to occur. This might be a button’s click event, for example.
LoadCompleteAt this point all controls are loaded. If you need to do additional processing at this time you can do so here.
PreRenderAllows final changes to the page or its control. This event takes place after all regular PostBack events have taken place. This event takes place before saving ViewState, so any changes made here are saved.
SaveStateComplete

Prior to this event the view state for the page and its controls is set. Any changes to the page’s controls at this point or beyond are ignored. This is useful if you need to write processing that requires the view state to be set.

Render

This is a method of the page object and its controls (and not an event). At this point, ASP.NET calls this method on each of the
page’s controls to get its output.
The Render method generates the client-side HTML, Dynamic Hypertext Markup Language (DHTML), and script that are necessary to properly display a control at the browser. This method is useful if you are writing your own custom control. You override this method to control output for the control.

UnLoad

This event is used for cleanup code. You use it to release any managed resources in this stage. Managed resources are resources
that are handled by the runtime, such as instances of classes created by the .NET common language runtime.

 
Sign Up to vote for this article
 
About Author
 
amalhashim
Occupation-Software Engineer
Company-Aditi Technologies
Member Type-Senior
Location-Not Provided
Joined date-07 Jun 2009
Home Page-http://lamahashim.blogspot.com
Blog Page-http://lamahashim.blogspot.com
I have done my masters in Computer Applications and graduation in Computer Science. I have great passion in working with Microsoft tool and technologies. I am also a Microsoft Most Valuable Professional. Personally my objective is to design/develop applications which eases user experience and performs better in long run.
 
 
Other popularSectionarticles
Comments
By:4Date Of Posted:4/12/2012 8:22:40 AM
a
5yr
Leave a Reply
Title:
Display Name:
Email:
(not display in page for the security purphase)
Website:
Message:
Please refresh your screen using Ctrl+F5
If you can't read this number refresh your screen
Please input the anti-spam code that you can read in the image.
^ Scroll to Top