Wait cursor for ASP.Net Ajax actions

Jan 18 2008

Suppose you have a page where you have a bunch of actions that invoke an Ajax callback. You can use the UpdateProgress control to indicate that the request is being invoked, for example to display some “please wait” text.

But that’s not it happens in the world of Winforms (or more broadly, desktop apps). You just get a wait cursor most of the time. The same happens when you do a normal postback (and some addition feedback too, granted).

It’s not very hard to add wait cursor support to your pages. Add the following script block right after the <asp:ScriptManager> tag:

<script type="text/javascript">

	var prm = Sys.WebForms.PageRequestManager.getInstance();
	prm.add_initializeRequest(InitializeRequest);
	prm.add_endRequest(EndRequest);

	function InitializeRequest(sender, args) {
	    document.body.style.cursor = 'wait';
	}

	function EndRequest(sender, args) {
	    document.body.style.cursor = 'default';
	}

</script>

This works separately from any UpdateProgress panels you have on the page. It also works for all UpdatePanel instances: for every Ajax request the cursor turns into an hourglass while the request is doing it’s work.

6 responses so far

  1. Nice script. It helped me. thanks.

  2. Great thing. Thanks a lot!

  3. some how doesnt work for me. its giving me an “invalid character” error. i m keeping the script right after the scripmanager tag, like u suggested. however i have an updateprogress already for another updatepanel. any idea why its not working??

  4. oh ya got it. copy paste problem. its the quotes. there are not single quotes what javascript uses :) . anyways nice stuff, thanks a lot.

  5. Thanks. Worked like a charm :)

  6. That is so good, thanks for sharing this code,

Leave a Reply