Article ID: 814745 - View products that this article applies to.
This article describes the AutoEventWireup attribute:
In Visual Studio .NET or in Visual Studio 2005, events are bound to event-handler methods using event delegates. If you use the Web Forms Designer to design Web Forms, the designer automatically generates code to bind events to their event-handler methods.
In Visual Basic .NET or in Visual Basic 2005, the designer performs this binding using the Handles keyword in the declaration of the event-handler method.
The following sample code illustrates the Handles keyword in Visual Basic .NET or in Visual Basic 2005:
In Visual C# .NET, the designer generates an explicit event delegate:
The ASP.NET page framework supports an alternative mechanism that uses the AutoEventWireup attribute of a Web Forms page to automatically associate page events and event-handler methods. If the AutoEventWireup attribute of the @ Page directive is set to TRUE (or if it is not specified because its default value is TRUE), the ASP.NET page framework automatically calls page event-handler methods.
For example, the Page_Init and Page_Load event-handler methods are explicitly called by the ASP.NET page framework, without using the Handles keyword or an explicit event delegate.
However, the drawback of using the AutoEventWireup attribute to automatically associate page events and their event-handler methods, is that event-handler methods must have standard, predefined names. This limits how you can name event-handler methods.
Visual Studio .NET and Visual Studio 2005 do not have the same limitation on method names. In Visual Studio .NET and in Visual Studio 2005, the default setting for AutoEventWireup is FALSE. Additionally, the designer always uses the Handles keyword or an explicit event delegate to bind events to their event-handler methods. As a result, you can use non-standard names for event-handler methods.
ConclusionWhen you explicitly set AutoEventWireup to TRUE, Visual Studio .NET or Visual Studio 2005, by default, generates code to bind events to their event-handler methods. At the same time, the ASP.NET page framework automatically calls the event-handler methods based on their predefined names. This can lead to the same event-handler method being called two times when the page runs. Therefore, Microsoft recommends that you always set AutoEventWireup to FALSE while working in Visual Studio .NET.
For more information, visit the following MSDN Web sites:
ASP.NET Server Control Event Model
Article ID: 814745 - Last Review: December 3, 2007 - Revision: 3.5