Solved

Disable a button from the user clicking it twice

Posted on 2014-02-05
6
360 Views
Last Modified: 2014-04-20
I have button in my form. I want the user to not be able to double click it or click it twice.

    <asp:Button ID="Button6" runat="server" Text="Cancel"  CssClass="cancelclass"  />

I need the code behind server side code to be executed on the button click, but at the same time not allow the user to click the button again. please help
0
Comment
Question by:TrialUser
6 Comments
 
LVL 82

Assisted Solution

by:leakim971
leakim971 earned 125 total points
ID: 39835663
Try this :
<asp:Button ID="Button6" runat="server" Text="Cancel"  CssClass="cancelclass" OnClientClick="if(window.flag) return false;window.flag=true;"  />

Open in new window

0
 
LVL 19

Assisted Solution

by:Albert Van Halen
Albert Van Halen earned 250 total points
ID: 39835818
@leakim971 : you have to make sure that flag is being reset after a partial postback otherwise a second submit will fail.

Also if your form doesn't validate, clicking it again won't submit the form.

The default method of ASP.NET submitting the form = WebForm_OnSubmit. It returns true if the form can be submitted and takes care of validation.
I would hook in into that method in order to disable the button.

Put the code below in your page somewhere
	var old_WebForm_OnSubmit = WebForm_OnSubmit;
	WebForm_OnSubmit = function() {
		if(old_WebForm_OnSubmit()) {
			// disable the button here
		}
	};

Open in new window

0
 

Author Comment

by:TrialUser
ID: 39836958
AlbertVanHalen : I have more than one button that can submit the form. How do I make sure which form to disable?

Thanks
0
Highfive + Dolby Voice = No More Audio Complaints!

Poor audio quality is one of the top reasons people don’t use video conferencing. Get the crispest, clearest audio powered by Dolby Voice in every meeting. Highfive and Dolby Voice deliver the best video conferencing and audio experience for every meeting and every room.

 
LVL 19

Expert Comment

by:Albert Van Halen
ID: 39838292
A dirty way is to use inline asp.net tags in markup
var old_WebForm_OnSubmit = WebForm_OnSubmit;
WebForm_OnSubmit = function() {
	if(old_WebForm_OnSubmit()) {
		document.getElementById('<%= <yourButton>.ClientID %>').disabled = true;
	}
};

Open in new window

0
 
LVL 9

Assisted Solution

by:Sar1973
Sar1973 earned 125 total points
ID: 39838485
On double click event: document.getElementById("myButton").disabled = true;
0
 
LVL 19

Accepted Solution

by:
Albert Van Halen earned 250 total points
ID: 39838694
Additional note : you need to return true or false in your method.
var old_WebForm_OnSubmit = WebForm_OnSubmit;
WebForm_OnSubmit = function() {
	if(old_WebForm_OnSubmit()) {
		document.getElementById('<%= <yourButton>.ClientID %>').disabled = true;
		return true;
	} else
		return false;
};

Open in new window

0

Featured Post

How to run any project with ease

Manage projects of all sizes how you want. Great for personal to-do lists, project milestones, team priorities and launch plans.
- Combine task lists, docs, spreadsheets, and chat in one
- View and edit from mobile/offline
- Cut down on emails

Join & Write a Comment

JavaScript can be used in a browser to change parts of a webpage dynamically. It begins with the following pattern: If condition W is true, do thing X to target Y after event Z. Below are some tips and tricks to help you get started with JavaScript …
This article discusses how to create an extensible mechanism for linked drop downs.
The viewer will learn the basics of jQuery, including how to invoke it on a web page. Reference your jQuery libraries: (CODE) Include your new external js/jQuery file: (CODE) Write your first lines of code to setup your site for jQuery.: (CODE)
The viewer will learn the basics of jQuery including how to code hide show and toggles. Reference your jQuery libraries: (CODE) Include your new external js/jQuery file: (CODE) Write your first lines of code to setup your site for jQuery…

747 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

16 Experts available now in Live!

Get 1:1 Help Now