Click here to Skip to main content
6,629,885 members and growing! (24,283 online)
Email Password   helpLost your password?
Web Development » ASP.NET » Utilities     Intermediate License: The Code Project Open License (CPOL)

ClickOnce JavaScript

By Stephen Inglish

Some JavaScript to add to a project to ensure buttons are clicked only once
C#, Javascript, HTML, XHTML, ASP, ASP.NET, WebForms, Ajax, Dev
Version:3 (See All)
Posted:25 Jun 2009
Views:3,227
Bookmarked:14 times
Announcements
Loading...
 
Search    
Advanced Search
Add to IE Search
printPrint   add Share
      Discuss Discuss   Broken Article?Report  
2 votes for this article.
Popularity: 0.90 Rating: 3.00 out of 5

1
1 vote, 50.0%
2

3
1 vote, 50.0%
4

5

Introduction

Many times, a developer wants the clicking of one button on the screen to disable the other buttons on the screen to prevent multiple submissions. Attached is some JavaScript that provides this feature.

Using the Code

Over the internet, there are several ways to prevent multiple button clicks in ASP.NET, this is just another way to do it.  Click here in order to see a server side control doing a similar thing that this JavaScript is doing.

<script language="JavaScript">
<!--
        /// <summary> 
        /// This function disables the submission buttons on the page 
        /// after checking to see if the page's validators are valid.  
        /// If they are not, then the changes don't take place.  
        /// The purpose of this method is to prevent the user from having 
        /// multiple submissions by clicking rapidly on the same button.
        /// </summary>
        function DisableButtonClick(valGroup) {
            // Start the validation false
            var cont = false;

            // Check to see if the page has Validators to fire
            if (typeof Page_ClientValidate != "undefined") {
                // There are validators available, 
	       // so fire them based on the ValidationGroup passed in
                cont = Page_ClientValidate(valGroup);
            }
            else {
                // There are no validators to fire, just disable the buttons
                cont = true;
            }
            // Only continue if the validators are valid or 
	   // there are no validators on the page
            if (cont) {
                // Find a button, and if it exists on this page, then disable it
                var btn = document.getElementById('<%= BtnSaveGeneral.ClientID %>');
                if (btn != null) {
                    btn.disabled = true;
                    btn.value = 'Saving...';
                }
            }
        }
-->
    </script>

Usage of this would be inline in HTML.

<asp:Button ID="BtnSave" runat="server" Text="Save" OnClick="BtnSaveGeneral_Click"
       	OnClientClick="DisableButtonClick('Save');" 
	UseSubmitBehavior="false" ValidationGroup="Save" />

Or in the code behind by changing the "onclientclick" attribute to the button.

BtnSave.Attributes.Add("onclientclick", "DisableButtonClick('Save');
BtnSave.UseSubmitBehavior = false;

Set the UseSubmitBehavior to false so ASP.NET will render JavaScript to allow the Button_Click event to fire on the server even though the JavaScript disables the button.

You can send in a blank ValidationGroup too if you want, by sending in an empty string.

Points of Interest

This JavaScript function explicitly names the buttons to clear inside the function.  Other options would be to pass into the function the buttons to disable when you call the OnClientClick, or to register each button that you wish to disable into an array and iterate through the array of names, disabling each control found.

History

  • 25th June, 2009: Initial post

License

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

About the Author

Stephen Inglish


Member

Occupation: Software Developer
Company: Sogeti USA
Location: United States United States

Other popular ASP.NET articles:

Article Top
You must Sign In to use this message board.
FAQ FAQ 
 
Noise Tolerance  Layout  Per page   
 Msgs 1 to 5 of 5 (Total in Forum: 5) (Refresh)FirstPrevNext
GeneralMy vote of 2 Pinmembertec-goblin4:40 30 Jun '09  
GeneralNo reason for dealing with ClientID PinmemberSpeednet_13:15 26 Jun '09  
GeneralLANGUAGE attribute for SCRIPT element is depreciated Pinmemberdrobosson20:37 25 Jun '09  
GeneralRe: LANGUAGE attribute for SCRIPT element is depreciated Pinmemberspoodygoon4:14 26 Jun '09  
GeneralClickOnce is a Microsoft deployment technology PinmemberAxel Rietschin14:24 25 Jun '09  

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

PermaLink | Privacy | Terms of Use
Last Updated: 25 Jun 2009
Editor: Deeksha Shenoy
Copyright 2009 by Stephen Inglish
Everything else Copyright © CodeProject, 1999-2009
Web22 | Advertise on the Code Project