Notice: This website is an unofficial Microsoft Knowledge Base (hereinafter KB) archive and is intended to provide a reliable access to deleted content from Microsoft KB. All KB articles are owned by Microsoft Corporation. Read full disclaimer for more details.

How to use the AutoEventWireup attribute in Visual Basic .NET


View products that this article applies to.

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.


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

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>
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" %>

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:
  1. Start Visual Studio .NET.
  2. On the File menu, point to New, and then click Project.
  3. In the New Project dialog box, click Visual Basic Projects under Project Types, and then click ASP.NET Web Application under Templates.
  4. 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.
  5. In Solution Explorer, right-click the WebForm1.aspx file, click Rename, and then type EventWireUpFalse.aspx.
  6. 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>
  7. 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
    
  8. 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:
  1. Visual Studio .NET.
  2. On the File menu, point to New, and then click Project.
  3. In the New Project dialog box, click Visual Basic Projects under Project Types, and then click ASP.NET Web Application under Templates.
  4. 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.
  5. In Solution Explorer, right-click the WebForm1.aspx file, click Rename, and then type EventWireUpTrue.aspx.
  6. 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>
  7. 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
  8. 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:

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


References

For additional information, click the following article numbers to view the articles in the Microsoft Knowledge Base:
303247 Overview of the ASP.NET code-behind model
312311 How to work with code-behind class files in an ASP.NET application by using Visual Basic .NET
For more information, visit the following MSDN Web sites:

↑ Back to the top


Keywords: KB317690, kbwebforms, kbhowtomaster, kbconfig

↑ Back to the top

Article Info
Article ID : 317690
Revision : 8
Created on : 4/29/2007
Published on : 4/29/2007
Exists online : False
Views : 569