Thursday, November 20, 2008

ASP.NET Web Server Control Event Model

When you first start working with ASP.Net, the Visual Studio wizard generates code that hides a lot of complexity of what happens behind the scenes. At first glance ASP.net has a model that is similar and familiar to WinForms developers – it allows you to hook up and respond to events generated from the UI. Obviously the big difference is that events in ASP.net get generated on a client and get processed on a server.

But when you start digging deeper you have to start wondering how events on the client hook back to your server code. In some cases it is via attributes attached to elements in the web-page.

<asp:Button ID="Button1" runat="server" OnClick="Button1_Click" Text="Button" />

Where Button1_Click is a method in the code behind.

But then there are other methods such as Page_Load that seem to get called without any kind of hook up.

protected void Page_Load(object sender, EventArgs e)

And Page_Load is not even an overridden method. So how does it get called?

The first thing to realize is that the first event is an example of an event triggered due to interaction with a control. The second is an example of a life-cycle driven event (life-cycle events can occur on both controls and pages).

Controls declared in the markup file (front-end) are automatically bound to the methods in the code behind class using the attributes defined on the control (such as OnClick was bound to Button1_Click in the above example).

As for page events, when you name a method using the standard Page_{EVENT NAME}, then the ASP.Net engine automatically calls these methods during the life cycle of the page. This is the reason that the Page_Load method gets called when ever the page gets loaded. This automatic hook-up is based on the “AutoEventWireup” property that is defined on the page and has a default value of true. If you prefer to manually hook-up the events, you can do so by setting “AutoEventWireup” to false.

<%@ Page Language="C#" AutoEventWireup="false" CodeBehind="Default.aspx.cs" Inherits="WebApplication2._Default" %>

Note:

If you use explicit event binding on your pages, then remember to set “AutoEventWireUp” to false, otherwise your events will get called twice.

Auto event binding is slow as ASP.net has to reflect on your code and find methods that match the standard. For this reason it is recommended that you do not use auto event binding on high traffic websites or when response times are important.

Learn more about the event model from MSDN

Learn about the page events and the list of page events that you can bind to at ASP.NET Page Life Cycle Overview (And here is another good article from CodeProject - http://69.10.233.10/KB/aspnet/ASPNET_Page_Lifecycle.aspx)

Finally, here is information for processing events on the client – (instead of on the server) - Client Script in ASP.NET Web Pages : useful for those times when you need to handle frequently fired events such as onmouseover.

No comments: