Click here to Skip to main content
15,886,137 members
Articles / Programming Languages / Javascript

Some Ajax Tips and Tricks

Rate me:
Please Sign up or sign in to vote.
3.49/5 (20 votes)
26 Feb 2007CPOL1 min read 74.2K   31   13
This code snippet shows how to display an hourglass on an AJAX web page, and also how to create a click once button

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:

JavaScript
<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:

JavaScript
<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)


Written By
Software Developer (Senior) Messages S.A.S
France France
I am Web Developer at Messages, a printing company in Toulouse, France. I am particularly interested about Blazor, but my primary development platform at work is ASP.NET MVC with C#. I have 15 years experience in developing software, always using Microsoft Technologies.

Comments and Discussions

 
GeneralMy vote of 5 Pin
77harliraha2-May-11 12:52
77harliraha2-May-11 12:52 
GeneralFantastic. Brief. To the point. Pin
Sean Devoy1-Feb-09 6:43
Sean Devoy1-Feb-09 6:43 
Generalvolks, what are you talking about? - this IS useful Pin
Arthur Juster9-Mar-07 3:26
Arthur Juster9-Mar-07 3:26 
GeneralRe: volks, what are you talking about? - this IS useful Pin
kenlarose8-Jun-07 10:45
kenlarose8-Jun-07 10:45 
QuestionIs this useful? Pin
ednrgc1-Feb-07 8:33
ednrgc1-Feb-07 8:33 
AnswerRe: Is this useful? Pin
aprenot1-Feb-07 9:07
aprenot1-Feb-07 9:07 
GeneralRe: Is this useful? Pin
ednrgc1-Feb-07 9:10
ednrgc1-Feb-07 9:10 
GeneralRe: Is this useful? Pin
aprenot1-Feb-07 9:16
aprenot1-Feb-07 9:16 
GeneralRe: Is this useful? Pin
ednrgc1-Feb-07 9:20
ednrgc1-Feb-07 9:20 
GeneralRe: Is this useful? Pin
Thyris8-Mar-07 10:42
Thyris8-Mar-07 10:42 
AnswerRe: Is this useful? Pin
Jan Seda1-Feb-07 9:14
professionalJan Seda1-Feb-07 9:14 
No, it's not useful at all. Only to mention his CV.

Sorry, but this should not be here and admins should remove this. Put this on blog but not as an article. I think Codeproject should start to introduce similar rules as MSDN otherwise CP will be full of mess.


Jan Seda
Security MVP
www.skilldrive.com, www.dotnetjob.com

GeneralRe: Is this useful? Pin
HellfireHD1-Feb-07 14:23
HellfireHD1-Feb-07 14:23 
AnswerRe: Is this useful? Pin
Daron Yndem1-Feb-07 12:11
Daron Yndem1-Feb-07 12:11 

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

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