2

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!

Pure.Krome
  • 84,693
  • 113
  • 396
  • 647

2 Answers2

3

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
  • 4,906
  • 23
  • 18
  • Just a minor clarification: the event wiring is not performed at runtime using reflection, it's wired as the ASPX is compiled – Cristian Libardo Nov 09 '08 at 16:45
  • 1
    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 Nov 09 '08 at 20:03
2

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
  • 4,906
  • 23
  • 18