Events fired during the Life Cycle of the of a page in Asp.Net 2.0

Hi

A few days back I talked about the event in the Asp.Net application Life cycle. It talked about the entire event that takes place when a request is made. You can read the article here. In this Article I am going to talk about the event that takes place in the Page.

PreInit()

In this event all the page level controls are created. The property assigned in the HTML is also assigned to the controls in this event. This event is only there for page and not present in user control (Hence not present in Master Page also). If we want to change the Theme and master page of a page, we need to override this event.

[Note: When you override this event remember to call the base pages preInit command.]

 

There is a big difference in the event when we have a master page for the page. If there is a Master page associated with the page then the controls on the page are not initialized. In this case the controls will be initialized only after the Init event. As I said earlier that master page does not have any PreInit event. Hence neither the master page nor any other control can be initialized at this event. For a deeper look at Master page check this post of mine

OnInit()

In this we have the property of the control,set at the design time. Remember in case of postback we will not have the changed values of the control(Values changed by user). If you want to get the changed value of the control in this event then we need to get it from the post data. Here is the code to get data from the post values.

string ChangedValue = Request.Form[IDOfTheControl].ToString();LoadViewState()

This is a conditional event. This event only fires when the page is posted back to the server. This event does not fire on the first load. The serialization of view state data form fields happen here. The view state data is data is loaded to the individual controls in this event. LoadPostBackData()

This is also a conditional event and only fires on postback. This event loads the data of the control, which implements IpostBackDataHandler from the HTTP Post data. Textbox is a good example of such control. Hence these controls will have data even if there view state is disabled. For a control like dropdown list, which gets it data at runtime only the selected value, is retained. Other values of the collection are not retained. 

Page_Load()

This is the most commonly know event of the Page. One thing to remember for this event is that the load event of the other user control is fired after the load method of the page. We can use the IsPostBack property to get whether this is the first load of the page or the page is posted back and add code to be executed only on the first load of the page. Control Event Handlers

This is also a conditional event and fires only on post back. If a user clicks on a button then the button_click is fired. Many a developer have confusion that the when a button is clicked only the click event is fired or page load event is fired after the click event is fired. Many controls (Like data grid) can emit multiple control events.

PreRender()

This event is the last chance to make any changes to the values of the control. This event will fire for all the child controls of the page recursively.

SaveViewState()

As the name suggest, in this event the ViewState is stored in the forms hidden

control. Render()

All the controls are rendered in this event. Again this event is called recursively for each control.


Unload()

Here you can have the page and controls perform clean-up operations. This event has no relevance besides clean up operations because the Page has already rendered. You can also not use any of the values present in the page in this event. 

Hope this helps
Thanks
Vikram

Feedback

Posted on 12/16/2006 11:33:23 PM

Try to refine your English skills and also better run anything you post thru a English grammer utility in any Word Document software.

Posted on 2/20/2007 2:25:48 AM

James Kovak : Can you be little more civilized in expressing your opinion? What is so bad with the english written? I don't think so it is bad at all.
Comment on the content and not on the writing skills... i am sure you are another hard nut racist from UK....

Posted on 3/11/2007 4:45:03 PM

I agree with you Mike. James Kovak : It is a bit rich coming from you when you use the word 'thru' in your post which, if we are being pedantic, was not an English word the last time I checked. Certainly if you'd taken your own advice and put your post through 'Word Document software' then you might have spotted this.

Posted on 3/29/2007 3:30:28 PM

Mr. Kovak, I have to say that Vikram's command of his second language seems to be greater than your command of your mother tongue. Thru? grammer? Get it together James. You are having delusions of adequacy. Thanks for the article, Vikram, it was quite informative. And don't let Mr. Eloquent bring you down.

Nick

Posted on 6/27/2007 6:30:33 AM

guys plz comment on the technical aspect...leave JAMES BOND...

Please post your comments:

Name:  
Email (optional): Your email address will not be posted.
URL (optional):
Comments: HTML will be ignored, URLs will be converted to hyperlinks  
Enter the text you see in the box:
 

Copyright © 2006 - 2012 Vikram Lakhotia