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 an ASP.NET Web Form by using Visual C# .NET


View products that this article applies to.

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.

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

Configuration of the AutoEventWireup attribute default values

The AutoEventWireup attribute can be declared in the <pages> section in the Machine.config file or the Web.config file. In either of these XML-based files, use the following syntax:
<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 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="true" %>

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

    Note Replace ServerName with the name of a server. MyWebApp is the name of 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="C#" AutoEventWireup="false" Inherits="MyWebApp.EventWireUpFalse" %>
    <HTML>
       <HEAD>
          <title>Visual C# .NET WIRE-UP FALSE</title>
       </HEAD>
       <BODY>
          <p><% Response.Write(message); %></p>
       </BODY>
    </HTML>
  7. 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="The Page_Load Event Fired with AutoEventWireup False";
          }
    
          // 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);
          }	
       }
    }

  8. 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.

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

    Note Replace ServerName with the name of a server. MyWebApp is the name of 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="C#" AutoEventWireup="true" Inherits="MyWebApp.EventWireUpTrue" %>
    <HTML>
       <HEAD>
          <title>Visual C# .NET WIRE-UP TRUE</title>
       </HEAD>
       <BODY>
          <p><% Response.Write(message); %></p>
       </BODY>
    </HTML>
  7. 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="The Page_Load Event fired with AutoEventWireup True";
          }      
     }
    }
  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 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.

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:

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


References

For additional information, click the following article numbers to view the articles in the Microsoft Knowledge Base:
303247 ASP.NET Code-Behind Model overview

312311 How to work with CodeBehind class files in ASP.NET


For more information, visit the following MSDN Web sites:

↑ Back to the top


Keywords: KB324151, kbwebforms, kbhowtomaster, kbevent

↑ Back to the top

Article Info
Article ID : 324151
Revision : 9
Created on : 4/29/2007
Published on : 4/29/2007
Exists online : False
Views : 481