Tuesday, March 30, 2010

ASP.NET Page Life Cycle


A page in an ASP.NET application consists of several server controls. These are the fundamental building blocks of an ASP.NET application. The Life cycle of an ASP.NET page, depends on whether the page is requested for the first time or it is a postback. Postback is a process by which a page can request for itself.

When the Page is requested for the first time

The Life Cycle of a page when requested for the first time:
Initializing: During this phase, the server creates an instance of the server control
Loading: During this phase, the instance of the control is loaded onto the page object in which it is defined.
PreRendering: During this phase, the control is updated with the changes made to it. This prepares the control for rendering.
Saving: During this phase, the state information of the control is saved. For example, if a value is set for the control during the Load event, it is embedded in the HTML tag that will be returned to the browser.
Rendering: During this phase, the server creates the corresponding HTML tag for the control.
Disposing: During this phase, all cleanup tasks, such as closing files and database connections opened by the control are performed.
Unloading: During this phase, all cleanup tasks, such as destroying the instances of server control are performed. This is the final event in the life cycle of a server control

Life cycle when the page processed during a postback event

The processing sequence in which a page is processed during a postback event is:


Initializing: During this phase, the server creates an instance of the server control
Loading view state: During this phase, the view state of the control posted by the client is reloaded into the new instance of the control.
Loading: During this phase, the instance of the control is loaded onto the page object in which it is defined.
Loading the postback data: During this phase, the server searches any data corresponding to the control that is loaded in the data posted by the client.
PreRendering: During this phase, the control is updated with the changes made to it. This prepares the control for rendering.
Saving state: During this phase, the change in the state of control between the current request and the previous request of the page is saved. For each change, the corresponding event is raised. For example, if the text of a textbox is changed, the new text is saved and a text_change event is raised.
Rendering: During this phase, the server creates the corresponding HTML tag for the control.
Disposing: During this phase, all cleanup tasks, such as closing files and database connections opened by the control are performed.
Unloading: During this phase, all cleanup tasks, such as destroying the instances of server control are performed. This is the final event in the life cycle of a server control
The events associated with the relevant page cycle phases are:

  • Page Initialization: Page_Init

  • View State Loading:LoadViewState

  • Postback data processing: LoadPostData

  • Page Loading: Page_Load

  • PostBack Change Notification: RaisePostDataChangedEvent

  • PostBack Event Handling: RaisePostBackEvent

  • Page Pre Rendering Phase: Page_PreRender

  • View State Saving: SaveViewState

  • Page Rendering: Page_Render

  • Page Unloading: Page_UnLoad
The following figure illustrates how the server controls on an ASP.NET page is processed by the server:



Thursday, March 25, 2010

Virtual Function Members Vs Abstract Function Members


This series is going to be a C# refresher that reminds you of C# features you sometimes forget.
A function can be marked as virtual when you don’t want your sub-classes to compulsorily provide implementation of some specialized form. So you can treat virtual as opposite of abstract, where you have to compulsorily provide implementation.
Consider this small fragment :
   1: public abstract class Person
   2: {
   3:     public abstract string Name {get;}
   4:     public virtual decimal TotalDebts
   5:     {
   6:         get
   7:         {
   8:             return 0;
   9:         }
  10:     }
  11: }
  12:  
  13: public class RichPerson : Person
  14: {
  15:     public override string Name 
  16:     {
  17:         get {return "I am Rich";}
  18:     }
  19: }
  20:  
  21: public class PoorPerson : Person
  22: {
  23:     public override string Name 
  24:     {
  25:         get {return "I am just a Poor guy";}
  26:     }
  27:     
  28:     public override decimal TotalDebts
  29:     {
  30:         get{return 1000000;}
  31:     }
  32: }
In this example the property “Name” declared in Line 3 is abstract while property “TotalDebts” in Line 4 is virtual.  The difference is while it is compulsory for deriving class to override “Name”, it is optional for deriving class to override “TotalDebts”. In this particular case PoorPerson is under heavy debt and implements TotalDebts to return his TotalDebts.

Saturday, March 20, 2010

What is Global.asax ? How to use it in .Net web projects ?

The Global.asx file is an optional file that contains code for responding to application level events raised by ASP.NET. This file is also called as ASP.NET application file. This file resides in the root directory of an application. If we are not defining this file in application, the ASP.NET page framework assumes that you have not defined any applicationa/Session events in the application.Click Here To Read More ...