AutoEventWireup Property

PagesSection.AutoEventWireup Property

 

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)

<ConfigurationPropertyAttribute("autoEventWireup", DefaultValue := True)>
Public Property AutoEventWireup As 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 a page event such as Load.

When AutoEventWireup is false, you must explicitly bind the event to a method. For example, if you have a Page_Load method in the code for a page, the method will be called in response to the Load event only if you write code like that in the following example (notice the Handles statement in Visual Basic and the event handler code in C#):

Partial Class AutoEventWireupExample
    Inherits System.Web.UI.Page
    Protected Sub Page_Load(ByVal sender As Object, _
            ByVal e As System.EventArgs) Handles Me.Load
        Response.Write("Executing Page_Load")
    End Sub
End Class

When AutoEventWireup is true, 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 as Page_Load or Page_Init. ASP.NET checks first for an overload that has the typical event-handler signature (that is, it specifies Object and EventArgs parameters). 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, as shown in the preceding example. 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 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 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.

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

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

' 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

.NET Framework
Available since 2.0
Return to top
Show:
© 2016 Microsoft