Why do we use AutoEventWireup="true" in our ASP.NET source code page directive?
<%@ Page Language="C#" AutoEventWireup="true" A CodeFile="Default.aspx.cs" Inherits="_Default" %>
Why do we use AutoEventWireup="true" in our ASP.NET source code page directive?
<%@ Page Language="C#" AutoEventWireup="true" A CodeFile="Default.aspx.cs" Inherits="_Default" %>
Know the answer? Post it — somebody with the same question will find it here.
Sign in to answer this question
It is the same account you read, post and publish with — and you will come straight back to this page.
Niradhip ChakrabortyPosted Aug 31, 2009, 4:10 AM
AutoEventWireup is a Boolean attribute that indicates whether the ASP.NET pages events are auto-wired
AutoEventWireup="true" means no need to register the event using += operator. It will be auto wired up.
AutoEventWireup="False" means u have to register the event using += operator
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)
In Machine.Config or WebConfig file this attribute can be declared as
As we know, when we change Machine.Config file, it will affect all asp.net webforms on the computer and if we change Web.config it will affect that application only. If you want this attribute change in a single webform change in page directive
Understanding its working
AutoEventWireup is false when we create a new web application and event handlers are automatically created. We can find this in the Initialize Component method
this.Load += new System.EventHandler(this.Page_Load);
Now declare a string public message in aspx.cs.
In aspx write . Give a string value for message in page_load ( message="Hi. How you doing").
Run the application and you can see that you are getting above mentioned message. Now comment the event handler for page_load in aspx.cs (let the AutoEventWireup attribute in default mode (false) only).
On running the application we 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. When AutoEventWireup is false the event handles are required for the page_load or page_init.
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.
You can see that in Machine.Config default value of this attribute is true and in page directive it is false.
So if page directive is not there then AutoEventWireup attribute value is true and page framework calls page events automatically.
If we set the value of this attribute to true, framework must make a call to CreateDelegate method for every Web Form (.aspx page).
So it will be a performance issue and should not set the value to true if performance is a key issue.