views:

800

answers:

2

Hi Folks, when i create an aspx page, the header includes something like this:-

<%@ Page
    Language="C#" 
    MasterPageFile="~/Views/Shared/Site.Master" 
    AutoEventWireup="true" 
    CodeBehind="Create.aspx.cs" 
    Inherits="My.Mvc.Views.Blah" %>

With ASP.NET MVC apps, do we:

  • need to include this AutoEventWireUp attribute?
  • What happens if we set this to false?
  • what does this attribute really do? is it valid for ASP.NET MVC?

thanks heaps folks!

+3  A: 

You can get rid of this attribute, or set it to false (which is the default).

AutoEventWireup means ASP.NET will use reflection at runtime to look for methods on your web form class in the form of Page_EventName (like Page_Load, Page_Init, etc), and automatically wire the methods to the corresponding page lifecycle events. I have some more details here: http://odetocode.com/Blogs/scott/archive/2006/02/16/2914.aspx

In MVC you should, as a general rule, avoid wiring up event handlers for the page lifecycle and code-behind in general.

OdeToCode
Just a minor clarification: the event wiring is not performed at runtime using reflection, it's wired as the ASPX is compiled
Cristian Libardo
AutoEVentWireup is a runtime wiring of the events. The code starts with HookUpAutomaticHandlers of the TemplateControl class, and the runtime behavior is well documented (http://msdn.microsoft.com/en-us/library/system.web.configuration.pagessection.autoeventwireup.aspx).
OdeToCode
+1  A: 

Sorry - the default is true in ASP.NET, so you should explicitly set AutoEventWireup to false in the @ Page directive, or remove it and set it to false in pages section of web.config for MVC.

OdeToCode
Awesome! cheers!
Pure.Krome