57

I don't understand what the AutoEventWireUp page property is responsible for.

I've read through this article, but even that I don't understand.

Saleh
  • 1,819
  • 1
  • 17
  • 44
chester89
  • 8,328
  • 17
  • 68
  • 113
  • Could you be more specific about what exactly you do not understand (in the article, for example) ? – Cerebrus Mar 25 '09 at 09:50
  • if I understand it right, it`s mentioned there that default value for that property can be true and can be false - what is that? – chester89 Mar 25 '09 at 09:57
  • You can Also Read [This](http://support.microsoft.com/kb/814745/en-us) – AminM Jul 13 '13 at 18:30

3 Answers3

64

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

Community
  • 1
  • 1
Cerebrus
  • 25,615
  • 8
  • 56
  • 70
  • 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 Mar 25 '09 at 10:07
  • but Page is an instance of the class Derived from System.Web.UI.Page, so this attribute is not inheritable? – chester89 Mar 25 '09 at 10:20
27

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 event handlers.

These are, in System.Web, version 2.0:

  • 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

System.Web, version 4.0, introduced a Page_PreRenderCompleteAsync for all classes derived from Page. That method, if present, will be registered using Page.RegisterAsyncTask and executed automatically "just before the PreRenderComplete event" (source: Page.ExecuteRegisteredAsyncTasks). This method seems very undocumented which suggest that it would be prefered to just call Page.RegisterAsyncTask with your own method instead.

sisve
  • 19,501
  • 3
  • 53
  • 95
  • Interesting answer, Simon. I like the details presented and it can sometimes be difficult to find this information. +1 – Cerebrus Mar 25 '09 at 10:19
10

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
  • 35,831
  • 5
  • 65
  • 110
  • 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 Mar 25 '09 at 09:58
  • Just fmoi: You gotta add the delegate to your list of Load event handlers in an overrided eventhandler prior to the Load Event, e.g. OnInit or OnPreInit. Perhaps better yet, you can add that line to an empty constructor that overrides the Page constructor. – user420667 Jul 16 '16 at 02:48