AutoEventWireup is an attribute of the @ Page directive. The AutoEventWireup attribute may have a value of true or false. The value is set to false when you create a new ASP.NET Web Application. This article describes how to set and to change the default values of the AutoEventWireup attribute. This article also describes some of the options of this attribute by using examples of the ASP.NET Web Forms code that is written in Microsoft Visual C# .NET.
You may use the AutoEventWireup attribute to code ASP.NET Web Forms and Web User Controls. When you set the value of the AutoEventWireup attribute to true, the code in ASP.NET Web Forms and in Web User Controls is simple. However, when you use the false value in certain circumstances, you may receive better performance.
You can specify a default value of the AutoEventWireup attribute in several places:
- The Machine.config file
- The Web.config file
- Individual ASP.NET Web Forms (.aspx files)
- Web User Controls (.ascx files)
When you use Microsoft Visual Studio .NET, the value of the AutoEventWireup attribute is set to false and the designer automatically generates event handlers. This article describes the default settings of the AutoEventWireup attribute and shows you some helpful code.
Requirements
This article assumes that you are familiar with the following topics:- Programming in ASP.NET
- Programming with Microsoft Visual C# .NET
- Microsoft Visual Studio .NET 2002 or Microsoft Visual Studio .NET 2003
Configuration of the AutoEventWireup attribute default values
The AutoEventWireup attribute can be declared in the <pages> section in the Machine.config file or the Web.config file. In either of these XML-based files, use the following syntax:<configuration>
<system.web>
<pages autoEventWireup="true|false" />
</system.web>
</configuration>
To change the value of the AutoEventWireup attribute in the individual ASP.NET Web Form, add the AutoEventWireup attribute to the @ Page directive, as follows:
<% @Page AutoEventWireup="true" %>
When the value of the AutoEventWireup attribute is false
If you want to manually hook up events to an event handler, set the value of the AutoEventWireup attribute to false. The following sample shows the code that you can use to handle the Load event of the Page object in an ASP.NET Web Form:- Start Microsoft Visual Studio .NET.
- On the File menu, point to New, and then click Project.
- In the New Project dialog box, under Project Types, click Visual C# Projects. Under Templates, click ASP.NET Web Application.
- In the Location box, type the project name
as
http://ServerName/MyWebApp.
Note Replace ServerName with the name of a server. MyWebApp is the name of a sample ASP.NET Web Application. - In Solution Explorer, right-click the WebForm1.aspx file, click Rename, and then type EventWireUpFalse.aspx.
- Replace the existing code in the EventWireUpFalse.aspx file
with the following code:
<%@ Page Language="C#" AutoEventWireup="false" Inherits="MyWebApp.EventWireUpFalse" %> <HTML> <HEAD> <title>Visual C# .NET WIRE-UP FALSE</title> </HEAD> <BODY> <p><% Response.Write(message); %></p> </BODY> </HTML>
- Replace the existing code in the EventWireUpFalse.aspx.cs
file with the following code:
using System; namespace MyWebApp { public class EventWireUpFalse : System.Web.UI.Page { public string message; private void Page_Load(object sender, System.EventArgs e) { message="The Page_Load Event Fired with AutoEventWireup False"; } // Visual C# .NET requires that you override the OnInit function, // adding a new delegate for the Page_Load event. override protected void OnInit(EventArgs e) { this.Load += new System.EventHandler(this.Page_Load); } } }
- On the Debug menu, click
Start to build and to run the ASP.NET Web
application.
In this example, you receive a message when the ASP.NET page framework raises the Page_Load event handler. If the value of the AutoEventWireup attribute is set to false, you must override the OnInit function, and then you must add a new delegate for the Page_Load event handler.
When the value of the AutoEventWireup attribute is true
When you set the value of the AutoEventWireup attribute to false, you must manually hook up events to event handlers. When you set the value of the AutoEventWireup attribute to true, the ASP.NET page framework can automatically raise events. The following sample describes how to code a Page_Load event handler in an ASP.NET Web Form when the value of the AutoEventWireup attribute is true.- Start Microsoft Visual Studio .NET.
- On the File menu, point to New, and then click Project.
- In the New Project dialog box, under Project Types, click Visual C# Projects. Under Templates, click ASP.NET Web Application.
- In the Location box, type the project name
as
http://ServerName/MyWebApp.
Note Replace ServerName with the name of a server. MyWebApp is the name of a sample ASP.NET Web Application. - In Solution Explorer, right-click the WebForm1.aspx file, click Rename, and then type EventWireUpTrue.aspx.
- Replace the existing code in the EventWireUpTrue.aspx file
with the following code:
<%@ Page Language="C#" AutoEventWireup="true" Inherits="MyWebApp.EventWireUpTrue" %> <HTML> <HEAD> <title>Visual C# .NET WIRE-UP TRUE</title> </HEAD> <BODY> <p><% Response.Write(message); %></p> </BODY> </HTML>
- Replace the existing code in the EventWireUpTrue.aspx.cs
file with the following code:
using System; namespace MyWebApp { public class EventWireUpTrue : System.Web.UI.Page { public string message; private void Page_Load(object sender, System.EventArgs e) { message="The Page_Load Event fired with AutoEventWireup True"; } } }
- On the Debug menu, click
Start to build and run the project.
In this example, you receive a message when the ASP.NET page framework raises the Page_Load event handler. If the value of the AutoEventWireup attribute is true, you do not have to override the OnInit function, and you do not have to add a new delegate for the Page_Load event handler.