Microsoft KB Archive/317690

= How to use the AutoEventWireup attribute in Visual Basic .NET =

Article ID: 317690

Article Last Modified on 4/29/2007

-

APPLIES TO


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

-



This article was previously published under Q317690



IN THIS TASK

 * SUMMARY
 * Requirements
 * Configure the default values of the AutoEventWireup attribute
 * Set the value of the AutoEventWireup attribute to false
 * Set the value of the AutoEventWireup attribute to true
 * Avoid setting the AutoEventWireup attribute to true when performance is key
 * Use the AutoEventWireup attribute in other ways
 * REFERENCES



SUMMARY
This article discusses 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. When an ASP.NET Web Application is created by using Microsoft Visual Studio .NET, the value of the AutoEventWireup attribute is set as false. This article describes how to set and to change the default values of the AutoEventWireup attribute. This article also explains the usage of this attribute with examples of ASP.NET Web Forms code that is written in Microsoft Visual Basic .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 outcome is simple code. If you set the value of the AutoEventWireup attribute to false under certain circumstances, the ASP.NET Web Application performs better.

You 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)

When you 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. This means that the Handles keyword in Visual Basic .NET does not have to be used in the server script in the Web Form.

By default, when the ASP.NET Web Application is created in Visual Studio .NET, the value of the AutoEventWireup attribute is set to false in the .aspx page and event handlers are automatically created. This article describes the default settings of the AutoEventWireup attribute and lists some helpful code.

back to the top

Requirements
This article assumes that you are familiar with the following topics:
 * Programming with ASP.NET
 * Programming with Visual Basic .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
 * Microsoft Windows 2000, Microsoft Windows XP, or Microsoft Windows Server 2003

back to the top

Configure the default values of the AutoEventWireup attribute
The value of the AutoEventWireup attribute can be declared in the section in the Machine.config file or the Web.config file as follows:    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.

The value of the AutoEventWireup attribute can also be changed in the individual Web Form. To change it in the Web Form, add the AutoEventWireup attribute to the @ Page directive, as follows: <% @Page AutoEventWireup=&quot;true&quot; %> back to the top

Set the value of the AutoEventWireup attribute to false
If you want to manually hook up events to a function, use the false value of the AutoEventWireup attribute. The following samples show the code that you can use to handle the Load event of the Page object in an ASP.NET Web Form:  Start Visual Studio .NET. On the File menu, point to New, and then click Project. In the New Project dialog box, click Visual Basic Projects under Project Types, and then click ASP.NET Web Application under Templates. In the Location box, type http:// /MyWebApp, and then click OK.

Note The  placeholder is a server name and MyWebApp is 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;vb&quot; AutoEventWireup=&quot;false&quot; Inherits=&quot;MyWebApp.EventWireUpFalse&quot;%> Visual Basic .NET WIRE-UP FALSE <% Response.Write(message) %> </li>  Replace the existing code in the EventWireUpFalse.aspx.vb file with the following code: Public Class EventWireUpFalse Inherits System.Web.UI.Page

Public message As String Public Sub Page_Load(ByVal Sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load 'MyBase.Load is the type for the Load event of the page. message = &quot;The Page_Load event fired with the AutoEventWireup attribute set to false&quot; End Sub

End Class </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 fires the Page_Load routine. Because the value of the AutoEventWireup attribute is set to false, you use the Handles keyword, and then follow it with a reference to the Load event: MyBase.Load.</li></ol>

back to the top

Set the value of the AutoEventWireup attribute to true
When you set the value of the AutoEventWireup attribute to false, you must manually hook up events to functions. On the other hand, when you set the value of the AutoEventWireup attribute to true, the ASP.NET page framework can automatically hook events. To code the Page_Load event handler in an ASP.NET Web Form when the value of the AutoEventWireup attribute is true, follow these steps: <ol> Visual Studio .NET.</li> On the File menu, point to New, and then click Project.</li> In the New Project dialog box, click Visual Basic Projects under Project Types, and then click ASP.NET Web Application under Templates.</li> In the Location box, type http:// /MyWebApp, and then click OK. Note: The  placeholder is a server name and MyWebApp is 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;vb&quot; AutoEventWireup=&quot;true&quot; Inherits=&quot;MyWebApp.EventWireUpTrue&quot;%> Visual Basic .NET WIRE-UP TRUE <% Response.Write(message) %> </li>  Replace the existing code in the EventWireUpTrue.aspx.vb file with the following code: Public Class EventWireUpTrue Inherits System.Web.UI.Page

Public message As String Public Sub Page_Load(ByVal Sender As System.Object, ByVal e As System.EventArgs) message = &quot;The Page_Load event fired with the value of the AutoEventWireup attribute set to true.&quot; End Sub

End Class </li> On the Debug menu, click Start to build and run the project.

Use the Page_Load event handler to display a message. However, in this case you do not have to use the Handles keyword because the ASP.NET page framework does this for you.</li></ol>

back to the top

Avoid setting the AutoEventWireup attribute to true when performance is key
You must not set the value of the AutoEventWireup attribute to true if performance is a key consideration. If you 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). Instead of relying on the automatic hookup, manually override the events from the page, as shown in code examples in this article.

For more information, visit the following Microsoft Developer Network (MSDN) Web site:

http://msdn.microsoft.com/library/en-us/dndotnet/html/dotnetperftips.asp

back to the top

Use the AutoEventWireup attribute in other ways
The AutoEventWireup attribute is also an attribute of the @ Control directive that is used in Web User Controls. You can use theAutoEventWireup attribute in ways that are similar to those in this article.

back to the top

<div class="references_section">