Events and ASP.Net

ASP.Net is an event-driven programming and responds to events through its predefined event handlers. When events are raised it must be handled. In ASP.NET, objects may raise events and may have assigned event handlers. For example, a button may raise the Click event and may have an OnClick method that handles the event. Besides these, there are other events in ASP.Net which fire when the page is requested. In this article, I will discuss the flow of these events.

“I have a Page which has a MasterPage and a UserControl in it. Can you tell me the flow, how the Init and Load events will be fired?”, one of my colleague asked me one day. Though i replied him, i was confused. So lets clear this confusion.

ASP.Net web page consists of
1. Page
2. MasterPage
3. UserControl.

Page is the parent control and MasterPage and UserControl reside within the page, so we can consider its child.
So when I say, if any particular event fire from Parent To Child, you need to understand that, event of Page, then MasterPage(if the page has) and finally UserControl event is fired.

Otherway round, when I say Child to Parent, then event of UserControl, followed by MasterPage and then Page event is fired.

INIT event is fired as “Child to Parent”.
So when I say this, If a page has UserControl, then UserControl’s Init event will be fired first, then only Page_Init event will be fired.
If a Page has MasterPage and UserControl in it, then the flow of INIT event would be UserControl, MasterPage INIT event and Page_Init. Since MasterPage and UserConttrol are within Page they are child of the page.

Consider a Page which has a UserControl within it. In this case Page_Load will be raised first followed by UserControl_Load Event.
If we consider a Page, which has MasterPage and UserControl within it, then the sequence of Load Event would be Page_Load, MasterPage_Load and finally UserControl_Load so basically we can say its Parent to Child event flow.

Except INIT event all other events in ASP.Net Page Life Cycle are fired from Parent To Child.

Events fired during POSTBACK
There are two flavors of postback events. The first is a changed event. This event fires when some piece of data is changed between postbacks. An example is the DropDownLists SelectedIndexChanged event, or the TextBox’s TextChanged event. Server controls that provide changed events must implement the IPostBackDataHandler interface. The other flavor of postback events is the raised event. These are events that are raised by the server control for whatever reason the control sees fit. For example, the Button Web control raises the Click event when it is clicked, and the Calendar control raises the VisibleMonthChanged event when the user moves to another month. Controls that fire raised events must implement the IPostBackEventHandler interface.


About Gokul Dahal, Nepal

Software Engineer, Freelancer, Design and Develop web applications,Software Developer Nepal, FreeLancer Nepal Contact :
This entry was posted in ASP.NET, C# and tagged , , , , , . Bookmark the permalink.

Leave a Reply

Fill in your details below or click an icon to log in: Logo

You are commenting using your account. Log Out /  Change )

Google+ photo

You are commenting using your Google+ account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )


Connecting to %s