• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 382
  • Last Modified:

Disable a button from the user clicking it twice

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
TrialUser
Asked:
TrialUser
4 Solutions
 
leakim971PluritechnicianCommented:
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
 
Albert Van HalenCommented:
@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
 
TrialUserAuthor Commented:
AlbertVanHalen : I have more than one button that can submit the form. How do I make sure which form to disable?

Thanks
0
VIDEO: THE CONCERTO CLOUD FOR HEALTHCARE

Modern healthcare requires a modern cloud. View this brief video to understand how the Concerto Cloud for Healthcare can help your organization.

 
Albert Van HalenCommented:
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
 
Sar1973Commented:
On double click event: document.getElementById("myButton").disabled = true;
0
 
Albert Van HalenCommented:
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

What does it mean to be "Always On"?

Is your cloud always on? With an Always On cloud you won't have to worry about downtime for maintenance or software application code updates, ensuring that your bottom line isn't affected.

Tackle projects and never again get stuck behind a technical roadblock.
Join Now