AutoEventWireUP role and its effects on Performance


"AutoEventWireUp" is a very familiar word to ASP.Net native developers. They can find it in the @Page directive at the top of the page. It can have values "true" or "false". Many times I have heard the question in interviews like what is AutoEventWireUp? What if we set its value to true or false?

Let's see what happens when it's values changes between true and false.

Case: AutoEventWireUp="true"

This is the default value for AutoEventWireUp attribute in C# and False for a VB.Net form so be specific while answering to somebody. You should know his/her context. When AutoEventWireup is true, all we need to do is follow the method naming convention of Page_EventToHandle. The ASP.NET runtime will automatically find and fire the method for the appropriate event.

One more point for ASP.Net life cycle and events; see here for details. Now look at the syntax of let's say the Page_Load event. Here Load is the method that raises the event.

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

Both are valid because I think there must be two delegates defined to handle the event with these two different handlers.

Now People say that from a performance point of view set the value of AutoEventWireUp to "false". Why?

Reason is here:

  • Page_PreInit
  • Page_Init
  • Page_InitComplete
  • Page_PreLoad
  • Page_Load
  • Page_LoadComplete
  • Page_DataBind
  • Page_SaveStateComplete
  • Page_PreRender
  • Page_PreRenderComplete
  • Page_Unload
  • Page_Error
  • Page_AbortTransaction
  • Page_CommitTransaction

This is the list of all some page events that occur during the page life cycle (some list items may be different from this list since it depends on the ASP.Net version). So what happens is that the runtime looks for these handlers when AutoEventWireUp="true". And since it supports both argument and non-argumented handlers. So the number of lookup calls are doubled. Now you may be concerned about performance if your site is having thousands of requests at a time and your runtime is searching for this stuff in between.

Case 2: AutoEventWireUp="false"

Now since the value is set to false the runtime won't do a lookup for the significant handlers. So now you have to explicitly bind the handlers with events.

Question: When and where will I bind the page events to its Handlers?

The answer is in the constructor of your class of the ASP.Net page.

public partial class _Default : Page
{
  
public _Default() // ctor
   {
      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)
   {
      
// ...
   }
}

Now you should have a look at this attribute when you are going to work on any ASP.Net WebApplication.
 
Happy Coding....
 


Similar Articles