Click here to Skip to main content
Email Password   helpLost your password?

Introduction

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 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"

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 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:

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.

Credits

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.

You must Sign In to use this message board.
 
 
Per page   
 FirstPrevNext
GeneralWhat is benefit if we do AutoEventWireUp as 'false'?
dipal_bhavsar
3:12 3 Jun '09  
Hi,

I just want to know what is the main benefit if we do AutoEventWireUp as 'false'.

As I know if we do AutoEventWireUp as 'false', our web page does not call any event which have been registered on code behind page. If I am not wrong could you tell me why and which circumstance we opt this option.

After all, your explanation is wonderful, but just I want to enhance my understanding in either case.

Thanks.

Dipal Bhavsar

Dipal Bhavsar
Software Professional

TietoEnator Software Technologies Pvt. Ltd.
Nordic Development Unit - Welfare
E-mail dipal.bhavsar@tieto.com

IT Tower, Road 12A, Kalyani Nagar,
Pune – 411 006, India

www.tietoenator.com

GeneralautoEventWireup="false" stops brekapoints
Mayank Parmar
21:19 26 Aug '08  
autoEventWireup="false" stops brekapoints

Regards,
Mayank Parmar
Software Engineer

GeneralA little correction in the article
Tabish
9:28 11 Jul '07  
When the autoeventwirup is set to false .Then the events are nor generated automatically.
then you have to register your event. IF the autoeventwireop is set to true events are generted automatically.

DO check this.
GeneralHow to get
Snijeesh
2:06 13 Apr '07  
How to get InitializeComponent method in .net 2005??

Watch...Wonder...
And make things happen..

GeneralCorrection
20:51 1 Mar '07  
when i tested the application by writing the stmt <%Response.Write(msg);%> in html page <head> tag it shows Compilation error .so use the stmt Response.Write(msg); in Page_Load to get the correct resultSmileSmile

Joxy
Generalthis article is missleading
PIAS
2:06 13 Feb '07  
hi, there is mistake in this article,
i have tried this code, but i'm not getting the output.

only if i set atribute to true and uncoment the code on Page_Load event then i get output in any other case i don't get output...


QuestionNo change
Stalee
7:47 12 Jan '07  
I have tested this article. I'm not finding any difference whether AutoEventWireup is true/false. I have tested ASP.NET with C#.

I'm getting the msg always.

Thanks in advance.



Regards...Stalin
GeneralGood article
Prasad_331
6:29 18 Sep '06  
I enjoyed reading this article. Very good for beginners.
GeneralGood One [modified]
visu1
4:08 24 Jun '06  
Superb Article

-- modified at 9:09 Saturday 24th June, 2006
Generalcorrection
Anonymous
2:16 9 Sep '05  
Correct me if I wrong, but when you say "I m in Page_Load" you should rather I HAVE BEEN in Page_Load, because the message is printed later in the cycle of events of the page.


Anyway, I m just being fussy Wink

Alex
Questiondidnt you mix things up ??
subtile
0:03 9 Sep '05  
This doesnt sounds right:

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.


_______________
Jesus Loves You
Generalwhy we do programming
Gul Mohd
5:10 25 Aug '05  
is there another alternate of programming i want to do something else
Unsure Unsure Unsure Unsure
GeneralRe: why we do programming
Amit Basra
7:28 1 Feb '07  
try doing something else like being an insurance surveyor for 2 years and getting insulted every day and swap roles with me . what say if u want a job as a surveyor in delhi contact me at 09350188228. and mail me if u can help me get a job as a programmer.
Questionhahahahah hoooo
Gul Mohd
5:08 25 Aug '05  
Big GrinBig GrinRoll eyesRoll eyesWTFWTFWTFLaughWink;PSniff:(SighCry:->Roll eyesLaughConfusedUnsure Unsure HmmmSuspiciousCoolRose
GeneralGood One...
maharishi_b
3:16 10 Jun '05  
Good article. very basic but very informative.

Thanxs

Maharishi

Nothing is Impossible. Even impossible spells "i m possible"


Last Updated 31 Jan 2005 | Advertise | Privacy | Terms of Use | Copyright © CodeProject, 1999-2010