Page Event handlers in ASP.net 2.0 - Raj Kaimal

Page Event handlers in ASP.net 2.0

In ASP.net 2.0, you can declaratively wire an event to a control by adding an attribute/value to your control. The attribute name is the event name and the attribute value is the method to call. VS 2005 will also create the handler for you as shown below. 

<asp:Button ID="Button1" runat="server" Text="Button" OnClick="Button1_Click" />

protected void Button1_Click(object sender, EventArgs e) {

Similarly, since we have a Page_Load event handler in the code behind, I expected to see some kind of declaration in the aspx code but found none.

Thankfully, MSDN explains this.

To create a handler for page events

In the code editor, create a method with the name Page_event.

For example, to create a handler for the page's Load event, create a method named Page_Load.

Page event handlers are not required to take parameters the way that other controls event handlers are.
 
ASP.NET pages automatically bind page events to methods that have the name Page_event. This automatic binding is configured by the AutoEventWireup attribute in the @ Page directive, which is set to true by default. If you set AutoEventWireup to false, the page does not automatically search for methods that use the Page_event naming convention.
http://msdn2.microsoft.com/en-us/library/6w2tb12s.aspx


So, if I wanted to handle the Page Init event, I just need to add this and the event gets bound automatically.

protected void Page_Init() {
   
Trace.Write("I am in init!");
}

void Page_Init() {
   
Trace.Write("I am in init!");
}

Published Monday, January 16, 2006 4:59 PM by rajbk
Filed under:

Comments

# re: Page Event handlers in ASP.net 2.0

A much better OOP practice is to override the OnLoad or OnInit methods instead of having an object subscribing to its own events.

Monday, January 16, 2006 7:45 PM by Fabrice

# re: Page Event handlers in ASP.net 2.0

Fabrice: It is doing this for you behind the scenes. In fact in 1.1 it is not even behind the scnes, you can view the code by expanding the hidden region.

You could always turn off the wire up and overload the method yourself: but why?

Thursday, February 09, 2006 7:07 PM by Roger

# re: Page Event handlers in ASP.net 2.0

Thanks to give such a good article.

I read many time about AutoEventWireup but understand after your article.

Monday, July 07, 2008 7:20 AM by Murlidhar Patil

# re: Page Event handlers in ASP.net 2.0

Thank you for posting this. I always wondered what AutoEventWireup meant

Thursday, July 24, 2008 1:49 PM by Dan

# re: Page Event handlers in ASP.net 2.0

Nice article,cleared my all doubts regarding AutoEventWireup.

Saturday, August 09, 2008 11:10 AM by GAURAV

# re: Page Event handlers in ASP.net 2.0

Really a very beautiful concet. I had a great problem that at which place we are declaring the eventhandler for page events in 2.o version. Now i am very very much clear.

Thanks a lot and lot

Monday, September 29, 2008 1:11 PM by Debu

# re: Page Event handlers in ASP.net 2.0

One of the problems with "magic" like this is that it requires extra documentation, not to mention extra infrastructure code that may cause problems.

I Just started seriously looking at ASP.Net and this specific issue is the only one to send me to google search so far. 30 minutes wasted.

Clearly this is a ASP.NET v1.x legacy.

Tuesday, November 11, 2008 7:35 AM by George

Leave a Comment

(required) 
(required) 
(optional)
(required)