Support us .Net Basics C# SQL ASP.NET Aarvi MVC Slides C# Programs Subscribe Download

What is AutoEventWireup in asp.net - Part 133

Suggested Videos
Part 130 - Refreshing cache automatically, when cached data is removed
Part 131 - Cache dependency on sql server database table 
Part 132 - Reload data into cache automatically when data in the table changes



Let us understand the use of AutoEventWireup property with an example. Create an asp.net web application project.

A webform in asp.net raises several events in it's life cycle. The following are few of those events.
1. Page Load
2. Page Load Complete
3. Page PreRender
4. Page PreRenderComplete



Copy and paste the following code in WebForm1.aspx.cs
protected void Page_Load(object sender, EventArgs e)
{
    Response.Write("Page Load <br/>");
}

protected void Page_LoadComplete(object sender, EventArgs e)
{
    Response.Write("Page LoadComplete <br/>");
}

protected void Page_PreRender(object sender, EventArgs e)
{
    Response.Write("Page PreRender <br/>");
}

protected void Page_PreRenderComplete(object sender, EventArgs e)
{
    Response.Write("Page PreRenderComplete <br/>");
}

Now in WebForm1.aspx, set AutoEventWireup=true
Run the application, and notice that the above event handler methods are executed as expected. We did not explicitly associate event handler methods to the events, but still the event handler methods are hooked up to their respective events. This is because we have AutoEventWireup attribute set to true.

Now, set AutoEventWireup=false
Run the application, and notice that none of the above event handler methods are executed. This is because, when "AutoEventWireup" property is set to false, the event handler methods does not get automatically associated with their respective events. We have to explicitly associate them by overriding OnInit() method as shown below. Now, copy and paste the following code in webform1.aspx.cs
protected override void OnInit(EventArgs e)
{
    this.Load += new EventHandler(Page_Load);
    this.LoadComplete += new EventHandler(Page_LoadComplete);
    this.PreRender += new EventHandler(Page_PreRender);
    this.PreRenderComplete += new EventHandler(Page_PreRenderComplete);
}

Run the application, and notice that the above event handler methods are executed as expected. 

Now, set AutoEventWireup=true

Run the application, and notice that every event handler method is executed twice. This is because, 
1. Setting AutoEventWireup=true, registered the event handler method's once, and
2. Overriding OnInit() method, has registered the same event handler method again

Important points to remember:
1. When AutoEventWireup is set to true and if you want the event handlers to be wired up with their events automatically, the event handler names should follow the standarad naming convention - Page_EventName.
2. AutoEventWireup can be set in the page directive or in web.config file.
3. To set autoEventWireup in web.config, use pages element as shown below.
<configuration>
   <system.web>
      <pages autoEventWireup="true" />
   </system.web>
</configuration>
4. If autoEventWireup, is set at both webform and web.config level, webform setting will take precedence over web.config setting.

So, AutoEventWireup is a boolean property which, when set to true, the page event handler methods are automatically wired with their respective events. If this property is set to false, then the event handler methods need to be explicitly associated with their respective events.

2 comments:

  1. hai venkat sir, i need to know deeply about WCF and MVC applications and your videos are excellent to understand, so will u able to provide that videos..please......

    ReplyDelete
  2. Sir your tutorial is really very good to restart my career again.....i can back again......

    ReplyDelete

It would be great if you can help share these free resources