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 topRequirements
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 topConfigure 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>
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="true" %>
back to the topSet 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.
back to the topSet 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.
back to the topAvoid 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:
back to the topUse 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 the
AutoEventWireup attribute in ways that are similar to those in this
article.
back to the
top