OdeToCode IC Logo

Thoughts On AJAX

Wednesday, August 10, 2005

Wally asks: “What is AJAX?”

I hope AJAX will be invisible. I hope developers don’t have to think about AJAX anymore than they think about the inner details of the HTTP protocol. I have no inside information on the ASP.NET team’s Atlas project yet, but I’m hoping to use it like so:

<asp:Image runat="server" ID="MagicImage"

           ImageUrl="32457.png"

           AsyncPostBack="true"

           OnDrag="MagicImage_OnDrag"

/>

The above snippet demonstrates two features:

1. High frequency events like ondrag, which we previously could only handle with client side script, will be available on the server.

2. The presence of an AsyncPostBack property indicating a control initiates a “lightweight” postback.

JavaScript would perform the lightweight postback asynchronously. The script should communicate back to the server with as little payload as possible, but with enough information (viewstate and other form values) to execute the typical page lifecycle.

On the server, life goes on as usual. We respond to events using .NET code, execute queries, data bind controls, set control properties - yadda, yadda. During the MagicImage_OnDrag event, we might set the control’s ImageUrl property to a new value. A new Page property, IsAsyncPostBack, will let us skip code that we know we don’t need to execute during a lightweight postback.

The server responds by grabbing rendered HTML for the controls marked with AsyncPostBack=”true” and piping bits back down the wire. Client side script parses instructions in the results and updates a subset of form fields and controls using the DOM.

All of the JavaScript I’m talking about is baked into the framework. All I need to do is set an AsyncPostBack property, wire up an event handler, and write C# code.

Too simplistic?