Thursday, April 19, 2007

AutoEventWireup




<%@ Page language="c#" Codebehind="WebForm1.aspx.cs" AutoEventWireup="true" Inherits="Sample2003Application.WebForm1" %>



AutoEventWireup -- It's a boolean field. By default in VS.NET 2003 it would be "false". More over this attribute is applicable only for applications which are created via VS.NET 2003.

If it's set to "True", the ASP.NET runtime does not require events to specify event handlers like Page_Load etc.,

Once you create a new webform in VS.NET the 'AutoEventWireup' attribute of that page would be false. Open the code behind file and check out the 'InitializeComponent' it would be something like this:

private void InitializeComponent()
{
this.Load += new System.EventHandler(this.Page_Load);
}


Within Page_Load even print a message into the screen.

private void Page_Load(object sender, System.EventArgs e)
{
Response.Write("We are in Page Load event.");
}

This message "We are in Page Load event." would be printed on this screen.

Now set the autoeventwireup attribute to "True" and then comment the below line:
this.Load += new System.EventHandler(this.Page_Load);

When the application is run we can still find the message getting printed on the screen. i.e., when it is true VS.NET automatically wires up the event for you.

It would be good to have a look at this article on Event Handling in ASP.NET

No comments: