Thursday, December 10, 2009

AutoEventWireup attribute in ASP.NET

. 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. In that case, no explicit Handles clause or delegate is needed.

. ASP.NET pages automatically bind page events to methods that have the name Page_event. This automatic binding is configured by the AutoEventWireup attribute in the @ Page directive, which is set to true by default. If you set AutoEventWireup to false, the page does not automatically search for methods that use the Page_event naming convention.
So, if I wanted to handle the Page Init event, I just need to add this and the event gets bound automatically.

protected void Page_Init() {
Trace.Write("I am in init!");
}

void Page_Init() {
Trace.Write("I am in init!");
}

.
AutoEventWireup="true" target is for page events only. In case of AutoEventWireup method are not case sensitive. (Page_Load or page_load both will work).

If AutoEventWireup="false" but still you want to executed Page event (Page_Load). In this you have to explicitly code for it.

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