Microsoft KB Archive/324151

= How to use the AutoEventWireup attribute in an ASP.NET Web Form by using Visual C# .NET =

Article ID: 324151

Article Last Modified on 4/29/2007

-

APPLIES TO


 * Microsoft ASP.NET 1.1
 * Microsoft ASP.NET 1.0
 * Microsoft Visual C# .NET 2002 Standard Edition
 * Microsoft Visual C# .NET 2003 Standard Edition

-



This article was previously published under Q324151



IN THIS TASK

 * SUMMARY
 * Requirements
 * Configuration of the AutoEventWireup attribute default values
 * When the value of the AutoEventWireup attribute is false
 * When the value of the AutoEventWireup attribute is true
 * When to avoid setting the value of the AutoEventWireup attribute to true
 * Other places where you can use the AutoEventWireup attribute
 * REFERENCES



SUMMARY
This article discusses the locations where the AutoEventWireup attribute can be used, the values that the AutoEventWireup attribute accepts, and how to use the AutoEventWireup attribute effectively in Microsoft ASP.NET Web Forms.

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 set the value of the AutoEventWireup attribute to true, the ASP.NET runtime does not require events to specify event handlers like the Page_Load event or the Page_Init event. This means that in Visual C# .NET, you do not have to initialize and to create the delegate structures.

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.

back to the top

Requirements
This article assumes that you are familiar with the following topics:
 * Programming in ASP.NET
 * Programming with Microsoft Visual C# .NET

The following list outlines the recommended hardware, software, network infrastructure, and service packs that you need:
 * Microsoft Visual Studio .NET 2002 or Microsoft Visual Studio .NET 2003

back to the top

Configuration of the AutoEventWireup attribute default values
The AutoEventWireup attribute can be declared in the section in the Machine.config file or the Web.config file. In either of these XML-based files, use the following syntax:    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 it belongs to.

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=&quot;true&quot; %> back to the top

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:// /MyWebApp.

Note Replace  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 .</li>  Replace the existing code in the EventWireUpFalse.aspx file with the following code: <%@ Page Language=&quot;C#&quot; AutoEventWireup=&quot;false&quot; Inherits=&quot;MyWebApp.EventWireUpFalse&quot; %> <HTML> <HEAD> Visual C# .NET WIRE-UP FALSE </HEAD> <BODY> <% Response.Write(message); %> </BODY> </HTML> </li>  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=&quot;The Page_Load Event Fired with AutoEventWireup False&quot;; }

// 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); }   } }

</li> 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.</li></ol>

back to the top

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. <ol> Start Microsoft Visual Studio .NET.</li> On the File menu, point to New, and then click Project.</li> In the New Project dialog box, under Project Types, click Visual C# Projects. Under Templates, click ASP.NET Web Application.</li> In the Location box, type the project name as http:// /MyWebApp.

Note Replace  with the name of a server. MyWebApp is the name of a sample ASP.NET Web Application.</li> In Solution Explorer, right-click the WebForm1.aspx file, click Rename, and then type EventWireUpTrue.aspx .</li>  Replace the existing code in the EventWireUpTrue.aspx file with the following code: <%@ Page Language=&quot;C#&quot; AutoEventWireup=&quot;true&quot; Inherits=&quot;MyWebApp.EventWireUpTrue&quot; %> <HTML> <HEAD> Visual C# .NET WIRE-UP TRUE </HEAD> <BODY> <% Response.Write(message); %> </BODY> </HTML> </li>  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=&quot;The Page_Load Event fired with AutoEventWireup True&quot;; }      } } </li> 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.</li></ol>

back to the top

When to avoid setting the value of the AutoEventWireup attribute to true
If performance is a key consideration, do not set the value of the AutoEventWireup attribute to true. The AutoEventWireup attribute requires the ASP.NET page framework to make a call to the CreateDelegate function for every ASP.NET Web Form page. Instead of using automatic hookup, you must manually override the events from the page. For more information, visit the following Microsoft Developer Network (MSDN) Web site:

http://msdn.microsoft.com/library/default.asp?url=/library/en-us/dndotnet/html/dotnetperftips.asp

back to the top

Other places where you can use the AutoEventWireup attribute
The AutoEventWireup attribute is also an attribute of the @ Control directive that is used in Web User Controls (.ascx) pages. You can use the AutoEventWireup attribute in ways that are similar to those that are described in this article.

back to the top

<div class="references_section">