![]() |
Web Development »
Ajax and Atlas »
General
Intermediate
License: The Code Project Open License (CPOL)
Some Ajax Tips and TricksBy desalbresThis code snippet shows how to display an hourglass on an AJAX web page, and also how to create a click once button |
Javascript, Windows, .NET 2.0, Ajax, VS2005, Dev
|
|
Advanced Search Add to IE Search |
|
|
|
||||||||||||||||
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.
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.
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>
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.
| You must Sign In to use this message board. | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
|||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
|||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
|||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
|||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
General
News
Question
Answer
Joke
Rant
Admin
|
PermaLink |
Privacy |
Terms of Use
Last Updated: 26 Feb 2007 Editor: Deeksha Shenoy |
Copyright 2007 by desalbres Everything else Copyright © CodeProject, 1999-2009 Web21 | Advertise on the Code Project |