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)
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.
Requirements
This article assumes that you are familiar with the following topics:- Programming with ASP.NET
- Programming with Visual Basic .NET
- Microsoft Visual Studio .NET 2002 or Microsoft Visual Studio .NET 2003
- Microsoft Windows 2000, Microsoft Windows XP, or Microsoft Windows Server 2003
Configure the default values of the AutoEventWireup attribute
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>
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="true" %>
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://ServerName/MyWebApp,
and then click OK.
Note The ServerName 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.
- Replace the existing code in the EventWireUpFalse.aspx
file with the following code:
<%@ Page Language="vb" AutoEventWireup="false" Inherits="MyWebApp.EventWireUpFalse"%> <html> <head> <title>Visual Basic .NET WIRE-UP FALSE</title> </head> <body><p><% Response.Write(message) %></p></body> </html>
- 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 = "The Page_Load event fired with the AutoEventWireup attribute set to false" End Sub End Class
- 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.
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:- 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://ServerName/MyWebApp, and then click OK. Note: The ServerName 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 EventWireUpTrue.aspx.
- Replace the existing code in the EventWireUpTrue.aspx file
with the following code:
<%@ Page Language="vb" AutoEventWireup="true" Inherits="MyWebApp.EventWireUpTrue"%> <html> <head> <title>Visual Basic .NET WIRE-UP TRUE</title> </head> <body><p><% Response.Write(message) %></p></body> </html>
- 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 = "The Page_Load event fired with the value of the AutoEventWireup attribute set to true." End Sub End Class
- 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.
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: