Information about the AutoEventWireup attribute (814745)



The information in this article applies to:

  • Microsoft Developer Network (MSDN)
  • Microsoft .NET Framework 1.1
  • Microsoft .NET Framework 1.0
  • Microsoft Visual Studio 2005 Professional Edition
  • Microsoft Visual Studio 2005 Standard Edition
  • Microsoft Visual Studio .NET (2003), Professional Edition
  • Microsoft Visual Studio .NET (2003), Enterprise Architect Edition
  • Microsoft Visual Studio .NET (2003), Enterprise Developer Edition
  • Microsoft Visual Studio .NET (2003), Academic Edition
  • Microsoft Visual Studio .NET (2002), Professional Edition
  • Microsoft Visual Studio .NET (2002), Enterprise Architect Edition
  • Microsoft Visual Studio .NET (2002), Enterprise Developer Edition
  • Microsoft Visual Studio .NET (2002), Academic Edition
  • Microsoft .NET Framework 2.0

SUMMARY

This article describes the AutoEventWireup attribute:
  • AutoEventWireup is a Boolean attribute that indicates whether events of a Web Forms page are autowired.
  • The default value for AutoEventWireup is TRUE, and can be set as follows:
    • TRUE when event-autowiring is activated.
    • FALSE when event-autowiring is not activated.

MORE INFORMATION

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:
Private Sub Page_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
In Visual C# .NET, the designer generates an explicit event delegate:
this.Load += new System.EventHandler(this.Page_Load);
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.

Conclusion

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

REFERENCES

For more information, visit the following MSDN Web sites:

Modification Type:MajorLast Reviewed:1/27/2006
Keywords:kbvs2005swept kbvs2005applies kbDesigner kbhelp kbDocs kbonline kbInetDev kbenv kbEvent kbCtrl kbControl kbWebForms kbProgramming kbdocerr KB814745 kbAudDeveloper