Disable button to prevent double clicking

B

Buddy Ackerman

If I put client side code to disable a button (so that a user doesn't click
it twice and double post the form) then teh button doesn't get posted with
the form and the button's server-side click event handler is not fired.
Seems like a big hole in the ASP.NET form handling process. Any way around
it and still use the button click event handler?


--Buddy
 
P

Parag

Hi,

You can keep a image and the button in the same div tag. Initially, make the
button visible and the image invisible. And when the user clicks the button
then hide the button and make the image visible. So in this situation user
will not be able to click the button twice and your problem will get solved.

Regards,
Parag Kulkarni
MTS | Persistent Systems Private Limited
 
S

S. Justin Gengo [MCP]

Buddy,

I built a javascript component that I give away for free (with source code).
One of the scripts in the component does exactly what you're attempting
here. You can see a sample and download the code from here:
http://www.aboutfortunate.com?page=javascriptdemo. You'll be interested in
demo# 3.

One thing that the code does is to call .NET's own javascript for checking
page validity before disabling the button. That way if a form isn't valid
the button doesn't get disabled which would strand the user.

If you have any questions let me know. The code is currently written for
..net 1.1 but is easily upgraded. If you're using 2.0 and have any difficulty
upgrading the code yourself let me know and I'll give you a hand. I just
haven't gotten around to posting updated code to my site yet.

--
Sincerely,

S. Justin Gengo, MCP
Web Developer / Programmer

www.aboutfortunate.com

"Out of chaos comes order."
Nietzsche
 
B

blackstaronline.net

<INPUT id="myButton" onclick="document.form1.myButton.disabled=true;"
type="button" value="Submit" name="myButton" runat="server"
onserverclick="myButton_Click">

The button will disable itself on click, and run the sub of your
choice.

Hope it helps,
Jeremy Reid
http://hgtit.com
 
Joined
Nov 3, 2006
Messages
1
Reaction score
0
Did that work?

I tried disabling the button as in above but once the button is disabled nothing else happens. I'm using jsp and java servlets. I'd still like to know what you did to get it to work in .ASP
 
Joined
Nov 7, 2007
Messages
1
Reaction score
0
ASP.NET solution

I created a subclass of Button to solve this problem in ASP.NET. Read all about it and download the source code from my blog.
 
Joined
Jan 8, 2008
Messages
1
Reaction score
0
prevent submitting form twice

There is a simple method of preventing multiple submits not only from one button but all buttons on page. For example someone may click OK button and then Cancel button - it is not double click on one button so previous solutions wont help.

1) put hidden field on page that will tell us if submit has been already made
<asp:HiddenField ID="HFSubmitForm" runat="server" Value="0" />

2) create javascript function that will check the value of hidden field and return false if submit has been made
<script language="javascript" type="text/javascript">
function checkSubmit()
{
// support clientside validation - if page is not valid framework wont fire
// postback
if ((typeof(Page_IsValid )!="undefined")&&(Page_IsValid == false))
{
document.getElementById("<%=HFSubmitForm.ClientID%>").value = 0;
return true;
}

// if postback - return false
if (document.getElementById("<%=HFSubmitForm.ClientID%>").value==1)
return false;
else
{
// mark that submit is to be done and return true
document.getElementById("<%=HFSubmitForm.ClientID%>").value = 1;
return true;
}
}
</script>

3) in every button on page add the following at begin of "OnClientClick" event:
if (checkSubmit()==false) {alert('form already submitted!'); return false;}

4) on Page_Load() in code behind file add the following line:
HFSubmitForm.Value = "0";

Thats it! It should work with popular browsers (opera, firefox, ie) and with clientside validation
 
Joined
Jan 11, 2008
Messages
1
Reaction score
0
Solution that works for every language

Hi all,

A lot of good solutions here, but this one works for everything (also for Java, JavaJoe :veryprou:).

In the onClick method of the button, set the button disabled and then manually submit the form (both using javascript).

EXAMPLE:
<INPUT name="myButton" type="submit" onclick="document.form1.myButton.disabled=true;document.form1.submit();">

Steve
 
Joined
Sep 24, 2010
Messages
1
Reaction score
0
This one will work :

<asp:Button id="submitButton" runat="server" Text="Submit" UseSubmitBehavior="false" OnClientClick="this.disabled=true; this.value = 'Please wait...';" />
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,755
Messages
2,569,537
Members
45,022
Latest member
MaybelleMa

Latest Threads

Top