Powered by Blogger.

Saturday, March 1, 2014

What is AutoEventWireup attribute in Asp.Net?

In this post we will discuss about what is AutoEventWireup attribute in Asp.Net?

You can also check my previous posts on:

- Virtual methods in C#.Net

- Sealed class and Sealed method in C#.Net

- View in sql server 2008

- AutoEventWireup attribute is a boolean attribute which is usually used in the Page directive.  The ASP.NET page framework supports an automatic way to associate page events and methods.

- If the AutoEventWireup attribute of the Page directive is set to true, the page framework calls page events automatically, specifically the Page_Init and Page_Load methods.

- If AutoEventWireup="false" but still you want to executed the Page_Load event then you have to explicitly code for it like below:

<form id="form1" runat="server" onload="Page_Load">

- When AutoEventWireup is true, handlers are automatically bound to events at run time based on their name and signature.

- The default value is true if AutoEventWireup is not specified in the @ Page directive.  Visual Studio automatically includes the attribute when it creates code-behind files.

For ASP.NET pages written in C#, Visual Studio sets the value to true. For Visual Basic, Visual Studio sets the value to false because handlers are bound to events by using the Handles keyword, which is inserted automatically by Visual Studio when it generates an event handler.

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