Click here to Skip to main content
Licence CPOL
First Posted 1 Feb 2007
Views 43,764
Bookmarked 31 times

Some Ajax Tips and Tricks

By desalbres | 26 Feb 2007
This code snippet shows how to display an hourglass on an AJAX web page, and also how to create a click once button
4 votes, 21.1%
1

2
1 vote, 5.3%
3
6 votes, 31.6%
4
8 votes, 42.1%
5
3.43/5 - 19 votes
μ 3.43, σa 2.73 [?]

Introduction

Microsoft Ajax 1.0 has just been released!

In synchronous web forms, it's easier to interact with the user, because he/she sees the page flashing back and processing (which is undesirable). With Ajax, you can add an update progress panel, and put an animated GIF inside. But what if you want to display an hourglass? In this article, I will show you two tricks.

Display an Hourglass

The following JavaScript code snippet nicely does the job:

<scriptmanager id="ScriptManager1" runat="server" />
<script language="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>

I have received several criticisms due to the length of this article, so I have decided to rename it as: some Ajax tips & tricks.

Click-once Button Control

Lots of attempts have been made to create a click once button...Some of them are quite complex like this one.

I've been searching the internet for a click once submit button, but without success. Some of them cause post-back, which is undesirable, while others don't work with validation controls. And it's so useful because it prevents users from clicking twice on the same button, thus leading to possible bugs and misleading behavior.

Combining the content of the first tip, we can achieve a good result, as shown in the picture below:

<scriptmanager id="ScriptManager1" runat="server" />
<script language="JavaScript">

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

var btn;

function InitializeRequest(sender, args) 
{
 document.body.style.cursor = "wait";                
 var btnId=args._postBackElement.id;
 btn= document.getElementById(btnId);
 if (btn.type=="button" || btn.type=="submit") 
   btn.disabled=true;
}

function EndRequest(sender, args) 
{                
 document.body.style.cursor = "default";    
 if(btn!=null) btn.disabled=false; 
}
</script>

Code Explanation

In the InitializeRequest function, we retrieve the pressed button id by retrieving the _postBackElement which is passed as an argument, and after we verify if it is a button.
We must declare the button variable as a global variable, to allow the EndRequest function to know which button fires the event.

Note that the above function will work ONLY if the button control is inside an UpdatePanel. And that's it! We have a click-once button! We can even put it in a MasterPage to propagate behavior to your content pages.

License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

About the Author

desalbres

Software Developer (Senior)
Messages S.A.S
France France

Member

Follow on Twitter Follow on Twitter
I am Web Developer at Messages, a printing company in Toulouse, France. I particularly enjoy ASP.NET MVC, JQuery, Silverlight...I have over 10 years experience in developing software, always using Microsoft Technologies.

Sign Up to vote   Poor Excellent
Add a reason or comment to your vote: x
Votes of 3 or less require a comment

Comments and Discussions

 
You must Sign In to use this message board. (secure sign-in)
 
Search this forum  
 FAQ
    Noise  Layout  Per page   
  Refresh
GeneralMy vote of 5 Pinmember77harliraha13:52 2 May '11  
GeneralFantastic. Brief. To the point. PinmemberSean Devoy7:43 1 Feb '09  
Generalvolks, what are you talking about? - this IS useful PinmemberArthur Juster4:26 9 Mar '07  
GeneralRe: volks, what are you talking about? - this IS useful Pinmemberkenlarose11:45 8 Jun '07  
QuestionIs this useful? Pinmemberednrgc9:33 1 Feb '07  
AnswerRe: Is this useful? Pinmemberaprenot10:07 1 Feb '07  
GeneralRe: Is this useful? Pinmemberednrgc10:10 1 Feb '07  
GeneralRe: Is this useful? Pinmemberaprenot10:16 1 Feb '07  
GeneralRe: Is this useful? Pinmemberednrgc10:20 1 Feb '07  
GeneralRe: Is this useful? PinmemberThyris11:42 8 Mar '07  
I agree. I found this useful, whether others did or not. Dead | X|
AnswerRe: Is this useful? PinmemberJan Seda10:14 1 Feb '07  
GeneralRe: Is this useful? PinmemberDoug K. Wilson15:23 1 Feb '07  
AnswerRe: Is this useful? PinmemberDaron Yöndem13:11 1 Feb '07  

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.

Permalink | Advertise | Privacy | Mobile
Web02 | 2.5.120210.1 | Last Updated 26 Feb 2007
Article Copyright 2007 by desalbres
Everything else Copyright © CodeProject, 1999-2012
Terms of Use
Layout: fixed | fluid