5,693,936 members and growing! (16,317 online)
Email Password   helpLost your password?
Web Development » Ajax and Atlas » General     Intermediate

Some Ajax tips and tricks

By desalbres

This code snippet shows how to display an hourglass on an AJAX web page, and also how to create a click once button
C#, VB, Javascript, XML, HTML, Windows, .NET 2.0, .NET, Ajax, Visual Studio, VS2005, Dev

Posted: 1 Feb 2007
Updated: 26 Feb 2007
Views: 21,543
Bookmarked: 19 times
Announcements
Loading...



Search    
Advanced Search
Sitemap
14 votes for this Article.
Popularity: 3.73 Rating: 3.26 out of 5
4 votes, 28.6%
1
0 votes, 0.0%
2
0 votes, 0.0%
3
5 votes, 35.7%
4
5 votes, 35.7%
5
Note: This is an unedited contribution. If this article is inappropriate, needs attention or copies someone else's work without reference then please Report This Article

Introduction

Microsoft Ajax 1.0 has just been released! (http://go.microsoft.com/fwlink/?LinkID=77296).

In syncronous 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 2 tricks.

Diplay 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 criticism 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 (http://www.codeproject.com/aspnet/ClickOnce_Button_Control.asp).
I've been searching the internet for a click once submit button, but wihout 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 to click 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 has no explicit license attached to it but may contain usage terms in the article text or the download files themselves. If in doubt please contact the author via the discussion board below.

A list of licenses authors might use can be found here

About the Author

desalbres



Occupation: Web Developer
Location: Brazil Brazil

Other popular Ajax and Atlas articles:

Article Top
Sign Up to vote for this article
You must Sign In to use this message board.
FAQ FAQ Noise ToleranceSearch Search Messages 
 Layout  Per page   
 Msgs 1 to 11 of 11 (Total in Forum: 11) (Refresh)FirstPrevNext
Generalvolks, what are you talking about? - this IS usefulmemberArthur Juster4:26 9 Mar '07  
GeneralRe: volks, what are you talking about? - this IS usefulmemberkenlarose11:45 8 Jun '07  
GeneralIs this useful?memberednrgc9:33 1 Feb '07  
GeneralRe: Is this useful?memberaprenot10:07 1 Feb '07  
GeneralRe: Is this useful?memberednrgc10:10 1 Feb '07  
GeneralRe: Is this useful?memberaprenot10:16 1 Feb '07  
GeneralRe: Is this useful?memberednrgc10:20 1 Feb '07  
GeneralRe: Is this useful?memberThyris11:42 8 Mar '07  
GeneralRe: Is this useful?memberJan Seda10:14 1 Feb '07  
GeneralRe: Is this useful?memberDoug K. Wilson15:23 1 Feb '07  
GeneralRe: Is this useful?memberDaron Yöndem13:11 1 Feb '07  

General General    News News    Question Question    Answer Answer    Joke Joke    Rant Rant    Admin Admin   

PermaLink | Privacy | Terms of Use
Last Updated: 26 Feb 2007
Editor:
Copyright 2007 by desalbres
Everything else Copyright © CodeProject, 1999-2008
Web18 | Advertise on the Code Project