views:

1125

answers:

3

Hi guys, I am rather new to ASP.NET and I can not understand what AutoEventWireUp page property is responsible for.
While serfing on the net, I have found a lot of forums discussions about that and they all seem discrepant to me.
Can you please explain cleanly what this property means exactly? I do not understand even this article support.microsoft.com

+2  A: 

As mentioned in the article, if you have AutoEventWireUp turned on, asp.net will automatically recognize you have a method with the page_load syntax and call it automatically:

private void Page_Load(object sender, System.EventArgs e)
{
}

This gives you a cleaner code behind at the expense of some (very) small overhead. Notice that if you don't specify it you must explicitly tell asp.net you want to handle the page load event:

this.Load += new System.EventHandler(this.Page_Load);

Note that this applies to other events in the page, as it uses a naming convention as *Page_Event*.

eglasius
This doesn't only apply to the Page_Load method. Other event-handler methods (eg, Page_Init, Page_PreRender etc) will be automatically wired-up too if AutoEventWireUp is on.
LukeH
+9  A: 

When a Page is requested, it raises various events which are considered to be part of it's lifecycle. I keep the visual representation created by Peter Bromberg handy with me.

The AutoEventWireUp property when True, automatically wires up some of these built-in events in the Page life cycle to their handlers. This means that you do not need to explicitly attach these events (using the Handles keyword, for instance, in VB).

Examples of these built-in events would be Page_Init and Page_Load.

If you set AutoEventWireUp to True and provide explicit wiring up of the EventHandlers, you will find them being executed twice! This is one reason why Visual Studio keeps this attribute set to false by default.

Edit: (after Chester89's comment)


It is useful to note that the default value of the AutoEventWireUp attribute of the Page is true, while the default value of the AutoEventWireUp property of the Page class is false

Cerebrus
so, if for page class it is false by default, I exprect it to be false by defalut also for the instance of this class. is that correct?
chester89
Quite right! ;-)
Cerebrus
but Page is an instance of the class Derived from System.Web.UI.Page, so this attribute is not inheritable?
chester89
+8  A: 

To add to previous answers; the automatic hooks are applied from TemplateControl.HookUpAutomaticHandlers. This method calls into TemplateControl.GetDelegateInformationWithNoAssert which contains which methods are considered as eventhandlers.

These are, in System.Web, Version 2.0.0.0, at the time of this post:

  • On all classes deriving from Page: Page_PreInit, Page_PreLoad, Page_LoadComplete, Page_PreRenderComplete, Page_InitComplete, Page_SaveStateComplete.
  • On all classes deriving from TemplateControl: Page_Init, Page_Load, Page_DataBind, Page_PreRender, Page_UnLoad, Page_Error.
  • Transaction support for all classes deriving from TemplateControl:
    • Page-AbortTransaction, or if it does not exist, OnTransactionAbort
    • Page-CommitTransaction, or if it does not exist, OnTransactionCommit
Simon Svensson
Interesting answer, Simon. I like the details presented and it can sometimes be difficult to find this information. +1
Cerebrus