What is AutoEventWireup ?

AutoEventWireup is a property in ASP.NET that controls the automatic binding of event handlers to the corresponding events in a web page. When AutoEventWireup is set to true, ASP.NET automatically maps event handlers to events based on a naming convention. This means that you don't have to explicitly wire up the event handlers in the code-behind file.

By default, AutoEventWireup is set to true in ASP.NET, which means that ASP.NET will search for event handlers that match the naming convention and automatically wire them up to the corresponding events during the page's initialization process. This can simplify the development process and reduce the amount of code required for event handling.

However, if AutoEventWireup is set to false, you need to manually specify the event handlers in the code-behind file using the Handles keyword or by wiring up the events in the Page_Init method. This gives you more control over the event handling process but requires additional coding.

The AutoEventWireUp property when True, automatically wires up some of these built-in events in the Page life cycle to their handlers. This means that you do not need to explicitly attach these events Examples of these built-in events would be Page_Init and Page_Load.

private void Page_Load(object sender, System.EventArgs e) { }

The value of AutoEventWireup = false means that your Page_Load event will not be automatically hooked to the page's Load event and so on for PreRender and the other page lifecycle events. It means in the constructor of your code-behind base class for the Page, you will have to manually do.

You can specify a default value of the AutoEventWireup attribute in several places:

  1. The Machine.config file
  2. The Web.config file
  3. Individual ASP.NET Web Forms (.aspx files)
  4. Web User Controls (.ascx files)

Example:

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" %>

In Visual Studio

When using Microsoft Visual Studio .NET, the default value of the AutoEventWireup attribute is set to false. In this case, the designer in Visual Studio generates the necessary event handlers automatically. However, if you choose to set AutoEventWireup to true, Visual Studio will generate code to bind the events and the page framework will automatically invoke the events based on their names. It's important to note that enabling AutoEventWireup in Visual Studio may result in duplicate execution of event code when the page is running. Therefore, it is generally recommended to keep AutoEventWireup set to false while working in Visual Studio to avoid such duplication of event code execution.

Disadvantage

One potential drawback of using the AutoEventWireup attribute is that it imposes a requirement for page event handlers to have specific and predictable names. This can limit your flexibility in choosing customized names for your event handlers. As a result, in Visual Studio, the AutoEventWireup attribute is typically set to false by default. In this scenario, the designer generates explicit code to establish the binding between page events and their corresponding methods, allowing for more control and customization in naming the event handlers.

Conclusion

It's important to note that while AutoEventWireup can make event handling easier, it may also impact performance, especially in large-scale applications. Therefore, it's recommended to consider the specific needs and requirements of your application when deciding whether to enable or disable AutoEventWireup.