AJAXing in .NET – #1 – Plain ASP.NET and ASP.NET AJAX

With the release of Microsoft Live Labs Volta, Nikhil Kothari’s recent update of Script# to support VS 2008, the ASP.NET AJAX Library’s new functionalities, and good ‘ol JavaScript—developers are not limited on AJAX solutions for the Microsoft platform. Given, JavaScript can be on any platform, I tend to focus more on .NET solutions.🙂

One of my holiday projects was to weigh and see exactly what went back and forth across the wire for each of these technologies and compare that to:

Implementation lines of code,

I don’t count using statements, headers, and “required” coding lines, simply those lines that implement the solution.

Intuititiveness of code,

Does the code make sense to write?

Browser support,

Does it work and look the same on IE 7.0, FireFox 2.0, Opera, and Safari?

Weight of transmissions (using Fiddler).

How much data is going across the line for comparable transactions?

Pitfalls?

What are the gotchas?

Download Source Code : Download Project Solutions (coming soon)

Note: This project contains two solutions, one for Volta project and one for all the others.

To make this a bit easier to read (and to write🙂 ), I’ve broken the content into a series of posts.

Example #2 – ASP.NET AJAX with UpdatePanels – The “default” implementation of the ASP.NET AJAX Framework—using UpdatePanels to manage partial rendering with “postbacks” still happening in the background.

Post 2:

Example #3 – Standard HTML and JavaScript – This is the “old” way to do things: directly manipulating the DOM.

Example #4 – ASP.NET AJAX Framework and JavaScript – Manipulating the DOM using the aliases and cross-browser compatibility features of the AJAX Framework.

Post 3:

Example #5 – Nikhil Kothari’s Script# – Script# is a scripting solution that sits on top of the C# development environment.

Example #6 – Microsoft Labs Live Volta – Previewed before, we’ll build the same example as the rest of the examples in Volta for comparison.

Now, let’s get started!

#1. Plain ASP.NET Page – Postbacks

Moving into .Net, let’s start with a simple ASP.NET page—no AJAX involved. This page has a drop down list (PhotoList) and an image control (PhotoPreview)—populating the image control OnChange of the drop down list.

Using Fiddler, an HTTP debugging tool, we can see that each time the page is requested, it posts back and grabs the image. It’s also important to note that there isn’t any caching at this point. Notice how events #39 and #43 request the same image and transmit it.

Using our scale above:

HTML Code: 9, Code Behind: 2

Being standard HTML and .NET code-behind, this is easily readable and makes sense.

As we can see from the image below, the page looks much the same on every browser (given, it’s pretty basic). The browsers, clockwise from top-left, are: Safari b3, IE 7, Opera 9, and FireFox 2.

Avoid postback “flash” is the most common, I think, to implement some sort of AJAX technology—cleaning up a UI and improving the user experience. The lack of caching (with the post back) is also a trouble; however, caching could be implemented with additional coding.

#2. ASP.NET AJAX with UpdatePanel

Our first method would be the standard ASP.NET AJAX technique—surrounding those two controls with an UpdatePanel and adding a ScriptManager to the page.

<asp:ScriptManagerID=”ScriptManager”runat=”server”/>

<asp:UpdatePanelID=”UpdatePanel”runat=”server”

UpdateMode=”Conditional”>

<ContentTemplate>

<asp:DropDownListID=”PhotoList”runat=”server”

AutoPostBack=”true”

OnSelectedIndexChanged=”PhotoList_SelectedIndexChanged”>

<asp:ListItemText=”ABCs – Blocks”Value=”abc_blocks.jpg”/>

<asp:ListItemText=”ABCs – Chalkboard #1″Value=”abc_board.jpg”/>

<asp:ListItemText=”ABCs – Chalkboard #2″Value=”abc_board2.jpg”/>

<asp:ListItemText=”Graduation”Value=”capandgown.jpg”/>

</asp:DropDownList>

<br/><br/>

<asp:ImageID=”PhotoPreview”runat=”server”Visible=”false”/>

</ContentTemplate>

</asp:UpdatePanel>

While our UI hasn’t changed, our HTTP traffic has changed quite a bit. Here we can see the WebResource and ScriptResource files pull down to the local client and cache as well as the first load of the page. What is interesting here is that on request #89, I’ve rerequested the capandgown.jpg image, but it never re-requests it, the cached image just loads on the client. Good deal and MUCH faster.

Using our scale above:

HTML Code: 14, Code Behind: 2

This is the basic way of implementing the ASP.NET AJAX Framework—enclosing existing controls in panels and letting partial rendering do it’s magic. I see this as very intuitive syntax.

Functionality is identical on all four platforms; this is because the ASP.NET AJAX framework handles browser support for us via ScriptManager.

As you can see from the Fiddler log, this page is extremely heavy—trading off smooth performance during usage for a resource load at the page load.