Click here to Skip to main content
14,302,993 members

AutoEventWireup attribute in Microsoft ASP.NET Web Forms

Rate this:
3.58 (50 votes)
Please Sign up or sign in to vote.
3.58 (50 votes)
30 Jan 2005CPOL
An introduction to the AutoEventWireup attribute in Microsoft ASP.NET Web Forms.


ASP.NET supports two methods to author pages:

  1. In-line code
  2. Code-behind

In-line code is code that is embedded directly within the ASP.NET page. Code-behind refers to code for your ASP.NET page that is contained within a separate class file. This allows a clean separation of your HTML from your presentation logic.

When we use Microsoft Visual Studio .NET to create ASP.NET Web Forms, code-behind pages are the default method. In addition, Visual Studio .NET automatically performs precompilation for us when we build our solution.

A little bit of background

Directives in ASP.NET control the settings and properties of page and user control compilers. They can be included anywhere on a page, although it is standard to place them at the beginning. Directives are used in both .aspx files (ASP.NET pages) and .ascx files (user control pages). ASP.NET pages actually support eight different directives.

  • @ Page
  • @ Control
  • @ Import
  • @ Implements
  • @ Register
  • @ Assembly
  • @ OutputCache
  • @ Reference

Page directives are the most commonly used directives, and are used to edit a wide variety of settings that control how the page parser and page compiler work. The following is a list of some of the more commonly used page directive attributes in ASP.NET.

@ Page language="c#" Codebehind="WebForm1.aspx.cs" 
         AutoEventWireup="false" Inherits="TestWebApp.WebForm1"
  • Language indicates the language in which the inline script code within the ASP.NET page is written (the code between <% %> tags). The value of this attribute can be C#, VB, or JS.
  • Codebehind indicates the name of the file being used as the code supporting this ASP.NET page. This file should reflect the Language setting; that is, if the language being used is C#, the CodeBehind file should have a .cs extension and be written in C#.
  • Inherits indicates a qualified class from which this ASP.NET page should inherit. Generally, this will be the name of the class described in the code-behind file.
  • AutoEventWireup is a Boolean attribute that indicates whether the ASP.NET pages events are auto-wired.

Note: In the above case, ASP.NET compiles the code-behind page on the fly. We have to note that this compilation step only occurs when the code-behind file is updated. Whether the file has been updated or not, well this is detected through a timestamp change.

To get to the Real Thing

The AutoEventWireup attribute may have a value of true or false. When an ASP.NET Web Application is created by using Microsoft Visual Studio .NET, the value of the AutoEventWireup attribute is set as false.

We can specify the default value of the AutoEventWireup attribute in the following locations:

  • The Machine.config file.
  • The Web.config file.
  • Individual Web Forms (.aspx files).
  • Web User Controls (.ascx files)

The value of the AutoEventWireup attribute can be declared in the <pages> section in the Machine.config file or the Web.config file, as follows:

<configuration> <system.web> <pages autoEventWireup="true|false" 

               /> </system.web> </configuration>

If you make these changes in the Machine.config file, the changes affect all ASP.NET Web Forms on the computer. If you make these changes in the Web.config file, the changes affect only the application that the file belongs to. However, to make changes in the individual Web Form Only, we have to add the AutoEventWireup attribute to the @ Page directive, as shown above.

Check out the Code

When we create a new ASP.NET Web Application in Visual Studio .NET, as mentioned earlier, by default, the value of the AutoEventWireup attribute is set to false in the .aspx page and event handlers are automatically created. We can find this in the InitializeComponent method:

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

The best way to see the working of this attribute would be:

  • Declare a string variable msg as public in WebForm1.aspx.cs.
  • In the HTML section of WebForm1.aspx, enter the following code in the <Head> section:
    <% Response.Write(msg); %>

    In the Page_Load, you could enter a value for the variable msg declared.

    msg= "We are in Page_Load()";

On running the application, you will get the message We are in Page_Load() [hereafter referred to as message]. Note: this is in the default case where the attribute is set to false.

Now try commenting the event handler code for the Page_Load in the aspx.cs file; and set the AutoEventWireup attribute to false in the .aspx page. On running the application this time, you will not get the message.

Now with the event handler code for the Page_Load in the aspx.cs file still commented; set the AutoEventWireup attribute to true in the .aspx page. On running the application this time, you will get the message.

Reason: In the case where AutoEventWireup attribute is set to false (by default), event handlers are automatically required for Page_Load or Page_Init. However, when we set the value of the AutoEventWireup attribute to true, the ASP.NET runtime does not require events to specify event handlers like Page_Load or Page_Init.

A thing to be kept in mind is that the AutoEventWireup attribute of the Page directive is set to true by default for the machine (check out the value of this attribute in the machine.config) but set to false by default for a .aspx page). So if it is missing, since by default it is true (i.e., at the machine level), the page framework calls page events automatically, specifically the Page_Init and Page_Load methods. In that case, no explicit Handles clause or delegate is needed.

Performance Issues

We must not set the value of the AutoEventWireup attribute to true if performance is a key consideration. If we set the value of the AutoEventWireup attribute to true, the ASP.NET page framework must make a call to the CreateDelegate method for every Web Form (.aspx page), and instead of relying on the automatic hookup, manually override the events from the page.


The whole thing was really simple but I just thought of posting it online; hope it helps someone. To give credit to where it is due. Most of the information was garnered from different places on the net. I've just complied them together and added a bit of my own to help folks along their way. I was also able to find an article related to this in MSDN Online (Article ID: 317690) but for VB.NET.


This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)


About the Author

Aby Thomas Varghese
Web Developer
United States United States
Aby Thomas Varghese lives in India, at Mangad in Kerala. He is a Software Engineer who has been working in the Software Field for some time now, in the city of Chennai. He works exclusively in Microsoft .NET platform with C# and ASP.NET. Previously he worked at Infortech Alliance, Cochin.

Prime interests: Graphics, AI, and Computer Games
Activities he loves: Working on his own projects, Listening to music, Reading anything interesting he can get his hands on.

Comments and Discussions

QuestionIs that an noticed mistake you have written ? Pin
peru j6-Sep-12 18:20
memberperu j6-Sep-12 18:20 
AnswerRe: Is that an noticed mistake you have written ? Pin
vaynenick5-Jun-16 19:00
membervaynenick5-Jun-16 19:00 
QuestionTRUE / FALSE Pin
RabindraNathTagore20-Jun-12 22:05
memberRabindraNathTagore20-Jun-12 22:05 
GeneralMy vote of 5 Pin
chandu sonkar16-Apr-12 17:38
professionalchandu sonkar16-Apr-12 17:38 
GeneralMy vote of 4 Pin
Aravind Ramesh2-Nov-10 1:27
memberAravind Ramesh2-Nov-10 1:27 
QuestionWhat is benefit if we do AutoEventWireUp as 'false'? Pin
dipal_bhavsar3-Jun-09 2:12
memberdipal_bhavsar3-Jun-09 2:12 
GeneralautoEventWireup="false" stops brekapoints Pin
Mayank Parmar26-Aug-08 20:19
memberMayank Parmar26-Aug-08 20:19 
GeneralA little correction in the article Pin
Tabish11-Jul-07 8:28
memberTabish11-Jul-07 8:28 
QuestionHow to get Pin
Snijeesh13-Apr-07 1:06
memberSnijeesh13-Apr-07 1:06 
GeneralCorrection Pin
JoxyJoy1-Mar-07 19:51
memberJoxyJoy1-Mar-07 19:51 
Generalthis article is missleading Pin
PIAS13-Feb-07 1:06
memberPIAS13-Feb-07 1:06 
QuestionNo change Pin
StalinMSEngineer12-Jan-07 6:47
memberStalinMSEngineer12-Jan-07 6:47 
GeneralGood article Pin
Prasad_33118-Sep-06 5:29
memberPrasad_33118-Sep-06 5:29 
GeneralGood One [modified] Pin
R.Viswanathan24-Jun-06 3:08
memberR.Viswanathan24-Jun-06 3:08 
Generalcorrection Pin
Anonymous9-Sep-05 1:16
memberAnonymous9-Sep-05 1:16 
Questiondidnt you mix things up ?? Pin
subtile8-Sep-05 23:03
membersubtile8-Sep-05 23:03 
Generalwhy we do programming Pin
Gul Mohd25-Aug-05 4:10
memberGul Mohd25-Aug-05 4:10 
GeneralRe: why we do programming Pin
Amit Basra1-Feb-07 6:28
memberAmit Basra1-Feb-07 6:28 
Questionhahahahah hoooo Pin
Gul Mohd25-Aug-05 4:08
memberGul Mohd25-Aug-05 4:08 
GeneralGood One... Pin
Maharishi Bhatia10-Jun-05 2:16
memberMaharishi Bhatia10-Jun-05 2:16 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.

Posted 26 Jan 2005


52 bookmarked