Friday, 20 April 2012

AutoEventWireup in ASP.NET: Why my ASP.NET events fire twice?

Here is a brief description of AutoEventWireup attribute in ASP.NET.

1. AutoEventWireup is an attribute in Page directive. 

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

We can specify the default value of the AutoEventWireup attribute in the following locations:

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

If you make these changes in the Machine.config file, the changes affect all ASP.NET Web Forms on the computer. If you make these changes in the Web.config file, the changes affect only the application that the file belongs to. However, to make changes in the individual Web Form Only, we have to add the AutoEventWireup attribute to the @ Page directive, as shown above.

2. AutoEventWireup is a Boolean attribute that indicates whether the ASP.NET pages events are auto-wired.

3. AutoEventWireup will have a value true or false. By default it is true in C# and false in VB.NET.
VB.NET has a mechanism for defining an event handler and subscribing to an event with the 'Handles' keyword.

Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
End Sub

There is no equivalent to the Handles keyword in C#. Thats why AutoEventWireup is by default true in C#.

protected void Page_Load(object sender, EventArgs e)
{
}

AutoEvenWireup = "TRUE"

The ASP.NET page framework supports an automatic way to associate page events and methods. If you do set AutoEventWireup to true, Visual Studio will generate code to bind the events and the page framework will automatically call events based on their names. In that case, no explicit Handles clause or delegate is needed.

AutoEventWireup = "FALSE"

If you do set AutoEventWireup to false, Visual Studio will not be able to generate code to bind the events. In this case, you must define explicit Handles clause or delegate.

public partial class _Default : Page
{
   public _Default()
   {
      Load += new EventHandler(Page_Load);
      PreInit += new EventHandler(Page_PreInit);        
   }
    protected void Page_Load(object sender, EventArgs e)
    {
      
    }
   protected void Page_PreInit(object sender, EventArgs e)
   {
     
   }
}

Why my ASP.NET events fire twice?

A very common question asked. The simple answer to this question is:

If you have AutoEventWiureUp="TRUE" as well as you also have defined delegates for the event handling in your code, .NET Framework will automatically call methods based on their names and also the methods you defined explicitly.

Disadvantage of AutoEventWireup = "TRUE"

1. The disadvantage of the AutoEventWireup attribute is that it requires that the page event handlers have specific, predictable names. This limits your flexibility in how you name your event handlers.

2. If you do set AutoEventWireup to true and also defined event handlers, Visual Studio will generate code to bind the events and the page framework will automatically call events based on their names. This can result in the same event code being called twice when the page runs. As a consequence, you should always leave AutoEventWireup set to false when working in Visual Studio.

3. Performance Issue: Another disadvantage is that performance is adversely affected, because ASP.NET searches for methods at run-time. For a Web site with high traffic volumes, the impact on performance could be significant.

1 comment:

  1. Ok... if setting it to true is not a good thing then why the heck is it even an option? If VB sets it false and handles its own events, then why on earth do we have the option to set it to true in C#. It's like telling someone not to touch the stove because it is hot. If you touch it you will get burned.

    When is MS going to wake up and do things right?

    ReplyDelete