Tim Van Wassenhove

Passionate geek, interested in Technology. Proud father of two

14 Dec 2008

Exploring AJAX on the ASP.NET platform

I finally found some time to experiment with AJAX on the ASP.NET platform. The first technique i looked into was Partial-Page Rendering with controls like UpdatePanel. It gave me an awkward feeling but even Dino Esposito, who spent a whole chapter on this technique in his book, seems to share that feeling.

Page methods, public static methods that are decorated with the WebMethodAttribute declared on a Page are exposed as a WebService method and return the result as JSON. An easy solution but it comes with the cost that it does not offer much flexibility.

Thanks to the WebHttpBinding and WebInvokeAttribute the Windows Communication Foundation now supports services that return JSON in a RESTfull call style. This is the technique that i prefer.

jQuery is a very sweet library that simplifies JavaScript development seriously and provides an easy way to consume WCF/JSON services easily. Here is an example of a page with a button that by default triggers a postback (supporting all users, even those without JavaScript) but that behavior is overriden with a XMLHTTP request instead once the document is loaded (an enhancement for users with JavaScript)

$(document).ready(function() {
	$('#RequestEchoButton').click(function() {
			type: 'POST',
			url: 'Default.svc/Echo',
			data: '{}',
			contentType: 'application/json; charset=utf-8',
			dataType: 'json',
			success: function(data) { $('#EchoResultDiv').html(data); },
			error: function(data) { $('#EchoResultDiv').html('Failed to request Echo.'); }
		return false;

Notice that my Default.svc page uses the WebServiceHostFactory

<%@ ServiceHost Language="C#" Debug="true" Service="PageServices.Default" Factory="System.ServiceModel.Activation.WebServiceHostFactory" %>

Conclusion: Unlike jQuery and WCF, I am not convinced that controls like UpdatePanel and ScriptManager add any value to my toolkit.