Tuesday, June 26, 2007

An ASP.NET Ajax UpdateProgress That Works

The ASP.NET Ajax UpdateProgress works ok if you're using the UpdatePanel to pass Ajax calls to your back end page code. But it doesn't work if you're making webservice calls from within javascript on your page.

It's easy to add this functionality to your web application however. All you need to do is to add handlers on Sys.Net.WebRequestManager to handle the invokeRequest and completedRequest events, like so:



Sys.Net.WebRequestManager.add_invokingRequest(onRequestInvoked);
Sys.Net.WebRequestManager.add_completedRequest(onRequestCompleted);



Make sure you do this after the ScriptManager control is loaded, otherwise you'll get an exception that Sys.Net doesn't exist. From there, your code can be as simple or as complicated as you like. Mine generally looks something like:


function onRequestInvoked() {
var stat = $get("status");
if (stat != null) stat.style.display = "block";
}

function onRequestCompleted() {
var stat = $get("status");
if (stat != null) stat.style.display = "none";
}


That's all there is to it!

0 comments: