Click to Rate and Give Feedback
MSDN
MSDN Library
.NET Development
.NET Framework 3.5
PagesSection Class
 AutoEventWireup Property

  Switch on low bandwidth view
This page is specific to
Microsoft Visual Studio 2008/.NET Framework 3.5

Other versions are also available for the following:
.NET Framework Class Library
PagesSection..::.AutoEventWireup Property

Updated: August 2008

Gets or sets a value indicating whether events for ASP.NET pages are automatically connected to event-handling functions.

Namespace:  System.Web.Configuration
Assembly:  System.Web (in System.Web.dll)
Visual Basic (Declaration)
<ConfigurationPropertyAttribute("autoEventWireup", DefaultValue := True)> _
Public Property AutoEventWireup As Boolean
Visual Basic (Usage)
Dim instance As PagesSection
Dim value As Boolean

value = instance.AutoEventWireup

instance.AutoEventWireup = value
C#
[ConfigurationPropertyAttribute("autoEventWireup", DefaultValue = true)]
public bool AutoEventWireup { get; set; }
Visual C++
[ConfigurationPropertyAttribute(L"autoEventWireup", DefaultValue = true)]
public:
property bool AutoEventWireup {
    bool get ();
    void set (bool value);
}
JScript
public function get AutoEventWireup () : boolean
public function set AutoEventWireup (value : boolean)

Property Value

Type: System..::.Boolean
true if events for ASP.NET pages are automatically connected to event-handling functions; otherwise, false. The default is true.

When AutoEventWireup is true, ASP.NET does not require that you explicitly bind event handlers to page events, such as Load or Init. Instead, handlers are automatically bound to events at run time based on their name and signature. For each event, ASP.NET searches for a method that is named according to the pattern Page_eventname, such asPage_Load or Page_Init. ASP.NET checks first for an overload that has the typical event-handler signature (Object and EventArgs). If an event handler with this signature is not found, ASP.NET checks for an overload that has no parameters.

When AutoEventWireup is false, you must explicitly bind event handlers to events. In that case, the method names do not have to follow a pattern.

The default value is true if AutoEventWireup is not specified in the @ Page directive. Visual Studio automatically includes the attribute when it creates code-behind files. For ASP.NET pages written in C#, Visual Studio sets the value to true. For Visual Basic, Visual Studio sets the value to false because handlers are bound to events by using the Handles keyword, which is inserted automatically by Visual Studio when it generates an event handler. If you set AutoEventWireup to true, you can omit (or remove) the Handles keyword.

Do not set AutoEventWireup to true if performance is a key consideration. When automatic event wireup is enabled, ASP.NET must make between 15 and 30 tries to match events with methods.

Note the following about binding event handlers to events:

  • If you set AutoEventWireup to true, make sure that you do not also manually attach page event handlers to events. If you do, handlers might be called more than one time.

  • Automatic binding is performed only for page events, not for events for controls on the page.

  • As an alternative to binding events to handlers, you can override the Oneventname methods of the page or of controls.

The following code example shows how to set or read the AutoEventWireup property in code.

Visual Basic
' Get the current AutoEventWireup property value.
Console.WriteLine( _
    "Current AutoEventWireup value: '{0}'", _
    pagesSection.AutoEventWireup)

' Set the AutoEventWireup property to false.
pagesSection.AutoEventWireup = False

C#
// Get the current AutoEventWireup property value.
Console.WriteLine(
    "Current AutoEventWireup value: '{0}'",
    pagesSection.AutoEventWireup);

// Set the AutoEventWireup property to false.
pagesSection.AutoEventWireup = false;

The following example shows the two forms of method signatures that are automatically attached to page events when AutoEventWireup is true.

Visual Basic
<%@ Page Language="VB" AutoEventWireup="false" CodeFile="Default.aspx.vb" Inherits="_Default" %>

Visual Basic
' This method will be automatically bound to the Load event
' when AutoEventWireup is true.
Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs)
    Response.Write("Hello world")
End Sub
' This method will be automatically bound to the Load event 
' when AutoEventWireup is true only if no overload having 
' object and EventArgs parameters is found.    
Protected Sub Page_Load()
    Response.Write("Hello world")
End Sub

C#
// This method will be automatically bound to the Load event
// when AutoEventWireup is true.
protected void Page_Load(object sender, EventArgs e)
{
    Response.Write("Hello world");

}
// This method will be automatically bound to the Load event 
// when AutoEventWireup is true only if no overload having 
// object and EventArgs parameters is found.
protected void Page_Load()
{
    Response.Write("Hello world");
}

C#
<%@ Page Language="C#" AutoEventWireup="false" CodeFile="Default.aspx.cs" Inherits="_Default" %>

        <pages autoEventWireup="true">
            <controls>
                <add tagPrefix="asp" namespace="System.Web.UI" assembly="System.Web.Extensions, Version=3.5.0.0, Culture=neutral, PublicKeyToken=31BF3856AD364E35"/>
                <add tagPrefix="asp" namespace="System.Web.UI.WebControls" assembly="System.Web.Extensions, Version=3.5.0.0, Culture=neutral, PublicKeyToken=31BF3856AD364E35"/>
            </controls>
        </pages>

The following example shows how to explicitly wire up events when AutoEventWireup is false.

Visual Basic
' The Handles keyword binds Page_Load to the Load event.
Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
    Response.Write("Hello world")
End Sub

C#
// Following are three alternative ways of binding an event
// handler to an event when AutoEventWireup is false.  For
// any given event do this binding only once or the handler
// will be called multiple times.

// You can wire up events in the page's constructor.
public _Default()
{
    Load += new EventHandler(Page_Load);
}

// You can override the OnInit event and wire up events there.
protected override void OnInit(EventArgs e)
{
    base.OnInit(e);
    Load += new EventHandler(Page_Load);
}

// Or you can override the event's OnEventname method and
// call your handler from there.  You can also put the code
// execute when the event fires within the override method itself.
protected override void OnLoad(EventArgs e)
{
    Page_Load(null, null);
    base.OnLoad(e);
}

protected void Page_Load(object sender, EventArgs e)
{
    Response.Write("Hello world");
}

Windows 7, Windows Vista, Windows XP SP2, Windows XP Media Center Edition, Windows XP Professional x64 Edition, Windows XP Starter Edition, Windows Server 2008 R2, Windows Server 2008, Windows Server 2003, Windows Server 2000 SP4, Windows Millennium Edition, Windows 98

The .NET Framework and .NET Compact Framework do not support all versions of every platform. For a list of the supported versions, see .NET Framework System Requirements.

.NET Framework

Supported in: 3.5, 3.0, 2.0

Date

History

Reason

August 2008

Added information to Remarks section.

Customer feedback.

Tags What's this?: Add a tag
Community Content   What is Community Content?
Add new content RSS  Annotations
Processing
© 2009 Microsoft Corporation. All rights reserved. Terms of Use  |  Trademarks  |  Privacy Statement
Page view tracker