Tag Archives: Asp.Net

Asp.Net Page Life Cycle – an understanding using msdn

Typical Asp.net page life cycle : Standalone page , with User Control Embedded , As Content Page within Master Page , As when User Control &  Content Page both on Master Page

What can be best source for understanding the asp.net page life cycle other than MSDN itself…? I am just going to refer the same and mentioned key points of for simple to understand and remember. Let’s start with Standalone aspx page… Then rest scenarios will be just change in the sequence of the events while getting called.

Typical Asp.net page life cycle: Standalone page

Page Life Cycle Events with calling sequence

·         PreInit

·         Init

·         InitComplete

·         PreLoad

·         Load

·         Control events

·         LoadComplete

·         PreRender

·         PreRenderComplete

·         SaveStateComplete

·         Render

·         Unload

Along with the events mentioned above, there some methods which are being called before and after the Events to facilitate functionality. Most of the times developers not aware ( don’t care !! ) about these methods other than other than specific cases like setting something before or after the method call or overloading them, so not covered in detailed.

Also please note that some of these methods are called recursively multiple times depending on the content of the page (e.g. postback  or not etc)


When is getting called

Page Methods getting called Before or during this event call


Typical use /Functionality handled by


Raised after the start stage is complete and
before the page initialization stage begins








Check IsPostBack property to determine
whether this is the first time the page is being processed

(If the request is a
postback, the values of the controls have not yet been restored from view
state. If you set a control property at this stage, its value might be
overwritten in the next event.


Typically event is useful for –

Create or re-create dynamic controls ;

Set a master page dynamically.

Set the Theme property dynamically.

Read or set profile property values.



Raised after all controls have been initialized and any skin
settings have been applied




To read or initialize control properties.


The Init event of individual controls
occurs before the Init event of the page.



Raised at the end of the page’s initialization stage.




Only one operation takes place between the Init and InitComplete
events which is tracking of view state changes is turned on.


View state tracking enables controls to persist
any values that are programmatically added to the ViewState collection.


 Until view
state tracking is turned on, any values added to view state are lost across


Controls typically turn on view state tracking
immediately after they raise their Init event.


Use this event to make changes to view state that
you want to make sure are persisted after the next postback.


Raised after the page loads view state for itself and all
controls after it processes postback data that is included with the Request instance.


LoadPageStateFromPersisteceMedium ( Postback Only)


LoadControlState(Postback only)


LoadViewState ( Postback Only)


ProcessPostData ( Postback Only)




only – first try)




The OnPreLoad method is called after all
postback data returned from the user is loaded.


At this stage in the page’s life cycle, view-state
information and postback data for declared controls and controls created
during the initialization stage are loaded into the page’s controls.


Controls created in the OnPreLoad method
will also be loaded with view-state and postback data.


The Page object calls the OnLoad method on the Page object, and then recursively does the same for each
child control until the page and all controls are loaded.


The Load event of individual controls occurs after the Load event of
the page



Use the OnLoad event method to set
properties in controls and to establish database connections.

Control events

TextChanged()  or Click() etc






Use these events to handle specific control
events, such as a Button control’s Click event or a TextBox 
control’s TextChanged event.


In a postback request, if the page contains
validator controls,

Check the IsValid property of the Page and
of individual validation controls before performing any processing.


Raised at the end of the event-handling stage.




Use this event for tasks that require that all
other controls on the page be loaded.


Raised after the Page object has created
all controls that are required in order to render the page, including child
controls of composite controls


The PreRender event of individual
controls occurs after the PreRender event of the page.




Use the event to make final changes to the
contents of the page or its controls before the rendering stage begins.


Raised after each data bound control whose DataSourceID property
is set calls its DataBind method.

Data Binding Events





Raised after view state and control state have been saved for
the page and for all controls







Any changes to the page or controls at this point affect
rendering, but the changes will not be retrieved on the next postback.


It is the processing Stage, not an event, Page
Object call this method on each control. All asp.net server controls have a
Render method, which
writes out the control’s markup to send to the






If you create a custom control, you typically override this
method to output the control’s markup.


However, if your custom control incorporates only standard
ASP.NET Web server controls and no custom markup, you do not need to override
 Render method. 

A user control (an .ascx file) automatically incorporates
rendering, so you do not need to explicitly render the control in code


Raised for each control and then for the page.




For Controls – use this event to do final cleanup for specific
controls, such as closing control-specific database connections.


For Page – use this event to do final cleanup work, such as
closing open files and database connections, or finishing up logging or other
request-specific tasks.


During the unload stage, the page and its controls have been
rendered, so you cannot make further changes to the response stream. If you
attempt to call a method such as the Response.Write method,
the page will throw an exception.


For those who don’t know View State Tracking

Control.TrackViewState Method It Causes tracking of view-state changes to the server control so they can be stored in the server control's StateBag object.  This object is accessible through the Control.ViewState property.Use this method when you have to develop template data-bound controls.

This method alerts ASP.NET to monitor changes to a server control's view state, which is required when you override the Control.DataBind method. The control then uses the IsTrackingViewState property to determine whether view-state change tracking is enabled  for the control. If it is not enabled, the TrackViewState method is called.

Ref – MSDN

Some important Event Methods in above Page Life cycle :

LoadControlState(postback only): The page recursively calls the LoadControlState method of those controls in its Controls collections that have called the RegisterRequiresControlState method of the page class to express interest in using their control states.

LoadViewState(postback only): The page first calls its own LoadViewState method and then recursively calls the LoadViewState method of the controls in its Controls collection to allow them to load their saved view states.

LoadPostData(postback only – first try): The page calls the LoadPostData method of the controls that implement the IPostBackDataHandler interface and passes the posted data into it. The LoadPostData method of each control must access the posted data and update the respective property of the control accordingly. For example, the LoadPostData method of the TextBox control assigns the new value of the text box to the Text property of the TextBox control.

LoadPostData(postback only second try): The page calls the LoadPostData method of those controls that where programmatically added to its Controls collection in the Load phase if they implement the IPostBackDataHandler interface.

RaisePostDataChangedEvent(Postback only): The page calls the RaisePostDataChangedEvent method of those controls whose LoadPostData method returned true. The RaisePostDataChangedEvent method raises post data changed event. For example, TextBox control raises this event when the new value of the text box is different from the old value.

RaisePostbackEvent (postback only): The page calls the RaisePostBackEvent method of the control whose associated HTML element submitted the form. For example, the Button control's associated HTML element posts the page back to the server. The RaisePostBackEvent method of a control must map the postback event to one or more server-side events. For example, the RaisePostBackEvent method of the Button control maps the postback event to the Command and Click server-side events.

Overall Page life cycle with the Events & methods

  • PreInit
  • Init
  • InitComplete
  • LoadControlState (Postback only)
  • LoadViewState (Postback only)
  • LoadPostData (Postback only – first try)
  • PreLoad
  • Load
  • LoadPostData (Postback only – second try)
  • RaisePostDataChangedEvent (Postback only)
  • RaisePostbackEvent (Postback only)
  • LoadComplete
  • RaiseCallbackEvent (Postback and Callback only)
  • PreRender
  • PreRenderComplete
  • SaveControlState
  • SameViewState
  • SaveStateComplete
  • Render


Now let’s say we have User Control on the asp.net webpage then Asp.net Page life cycle will be as follows

  • PreInit – Page
  • Init – UserControl
  • Init – Page
  • InitComplete – Page
  • LoadPageStateFromPersistenceMedium – Page 
  • ProcessPostData (first try) – Page
  • PreLoad – Page
  • Load – Page
  • Load – UserControl
  • ProcessPostData (second try) – Page
  • RaiseChangedEvents – Page
  • RaisePostBackEvent – Page
  • Control Events – Page
  • Control Event – UserControl
  • DataBinding – Page
  • DataBinding – UserControl
  • LoadComplete – Page
  • PreRender – Page
  • PreRender – UserControl
  • PreRenderComplete – Page
  • SaveViewState – Page
  • SavePageStateToPersistenceMedium – Page
  • SaveStateComplete – Page
  • Unload – UserControl
  • Unload – Page

Please refer to previous post for the Asp.net Life Cycle details


There can be the case that you have one Master page in which you asp.net web page is content page , in that case Asp.net LifeCycle will be as follows

  • PreInit – Page
  • Init – MasterPage
  • Init – Page
  • InitComplete – Page
  • LoadPageStateFromPersistenceMedium – Page 
  • LoadControlState (Postback only) – Page
  • LoadViewState (Postback only) – Page
  • ProcessPostData (first try) – Page
  • LoadPostData (Postback only – first try) – Page
  • PreLoad – Page
  • Load – Page
  • Load – MasterPage
  • ProcessPostData (second try) – Page
  • RaiseChangedEvents – Page
  • RaisePostBackEvent – Page
  • Control Events – OnClick,TextChanged,GridView Events  followed by DataBinding
  •     DataBinding – Page
  •     DataBinding – MasterPage
  •     DataBinding – UserControl
  •     DataBinding – ChildUserControl
  • LoadComplete – Page
  • PreRender – Page
  • PreRender – MasterPage
  • PreRenderComplete – Page
  • SaveViewState – Page
  • SavePageStateToPersistenceMedium – Page
  • SaveStateComplete – Page
  • Unload – MasterPage
  • Unload – Page

There is one scenario given by mbanavige on Asp.net Forums(http://forums.asp.net/t/1191194.aspx)  I am just going to refer the same ,

Our test setup is composed of a Page, MasterPage, UserControl, Nested UserControl and Button control as follows: The Page is tied to the MasterPage,The UserControl is on the Page,The Nested UserControl is on the UserControl,The Button is on the Nested UserControl.,Clicking the Button calls the Page.DataBind method

Then in above mentioned case asp.net page life cycle will be as follows

  • PreInit – Page
  • Init – Nested ChildUserControl
  • Init – UserControl
  • Init – MasterPage
  • Init – Page
  • InitComplete – Page
  • LoadPageStateFromPersistenceMedium – Page 
  • ProcessPostData (first try) – Page
  • PreLoad – Page
  • Load – Page
  • Load – MasterPage
  • Load – UserControl
  • Load – Nested ChildUserControl
  • ProcessPostData (second try) – Page
  • RaiseChangedEvents – Page
  • RaisePostBackEvent – Page
  • Click – Button – Nested ChildUserControl
  •    DataBinding – Page
  •    DataBinding – MasterPage
  •    DataBinding – UserControl
  •    DataBinding – Nested ChildUserControl
  • LoadComplete – Page
  • PreRender – Page
  • PreRender – MasterPage
  • PreRender – UserControl
  • PreRender – Nested ChildUserControl
  • PreRenderComplete – Page
  • SaveViewState – Page
  • SavePageStateToPersistenceMedium – Page
  • SaveStateComplete – Page
  • Unload – Nested ChildUserControl
  • Unload – UserControl
  • Unload – MasterPage
  • Unload – Page


Interview quickies:

1)    If we have a page called "Default" and a Master page called "DefaultMaster".
Both contain Page Load events. When a page request comes to Default page, which Page Load event will call first

Ans : Default.aspx Page load


2)    If we have a page called "Default" and a Master page called "DefaultMaster".
For both Page.Init events is handled. When a page request comes to Default page, which Page Init () event will call first


Ans:- DefaultMaster.Master page init event followed by Default.aspx

In Case of Nested Master Pages –

Main Master Page Init ===> Nested/Sub Master page Init ===>

Content Page Init /Requester Page.aspx


If you are still unsatisfied with asp.net life cycle events

Reference :




The handle is invalid. (Exception from HRESULT: 0x80070006 (E_HANDLE))

Recently while working on asp.net application , I got following exception

Server Error in ‘/’ Application.

The handle is invalid. (Exception from HRESULT: 0x80070006 (E_HANDLE))
Description: An unhandled exception occurred during the execution of the current web request. Please review the stack trace for more information about the error and where it originated in the code.

Exception Details: System.Runtime.InteropServices.COMException: The handle is invalid. (Exception from HRESULT: 0x80070006 (E_HANDLE))

Source Error:

An unhandled exception was generated during the execution of the current web request. Information regarding the origin and location of the exception can be identified using the exception stack trace below.

Stack Trace:

[COMException (0x80070006): The handle is invalid. (Exception from HRESULT: 0x80070006 (E_HANDLE))]

[FileLoadException: Could not load file or assembly ‘SMDiagnostics, Version=, Culture=neutral, PublicKeyToken=b77a5c561934e089’ or one of its dependencies. The handle is invalid. (Exception from HRESULT: 0x80070006 (E_HANDLE))]

System.ServiceModel.Activation.HttpModule.ProcessRequest(Object sender, EventArgs e) +0
System.Web.SyncEventExecutionStep.System.Web.HttpApplication.IExecutionStep.Execute() +60
System.Web.HttpApplication.ExecuteStep(IExecutionStep step, Boolean& completedSynchronously) +86

After much much of the time , I came to conclusion that following steps can able to solve the problem fast , at least it done the same for me.

First of all check –
Check if the assembly reference added under <assemblies> of Web.config
<add assembly=”SMDiagnostics, Version=, Culture=neutral, PublicKeyToken=B77A5C561934E089″ />


Verify if the file  SMDiagnostics.dll exists in the folder at location:
C:\windows\Microsoft.NET\Framework\v3.0\Windows Communication Foundation

Also Verify if the assembly is registered into the GAC :

If the file is there in GAC you can able to see                                                                                                                                   ‘SMDiagnostics, Version=, Culture=neutral,  PublicKeyToken=b77a5c561934e089’ in the list

If the answer for any of the two questions is no, then .Net framework WCF component may not be installed successfully on the machine. Please try reinstalling it.

If still you are not able to solve the exception,Check if your solution is referring to the any External Dll ,
which is not getting loaded due to some reason and eventually raising the exception.

This was exactly the same case with me.

I was using one COM component funcationality which was placed in dll , which is placed under BIN folder and
was using/accessing  the functions from COM in App_code  business code files. and path was mentioned in Web.config as Key like
<add key=”FuncPath” value=”C:\Inetpub\wwwroot\XYZ\APPLICATION\Bin\”></add>
and as the files in the BIN are not getting loaded , it was giving me the error.

After some following the issue for more than 3-4 hours of try and error  I found the reason that Under IIS , for Virtual Directory Properties Execution Permissions were marked as “Scripts and Executables” the dll is not being loaded and I was getting “System.Runtime.InteropServices.COMException”

After changing Execution Permissions to “Scripts Only” and applying it. it was started working fine.

I dont know the any special difference other than

Scripts only Run only scripts,such as ASP applications  and
Scripts and Executables Run both scripts, such as ASP applications, and executables

Although my problem is solved, I am still searching for difference… Anyone Please ……