472,358 Members | 1,979 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 472,358 software developers and data experts.

disable or prevent a button with onclick and javascript

I scoured this group and others looking for the best way to disable a
button after the first click to prevent multiple submissions, but
never did find anything that worked like they said it would. I went
ahead and wrote my own bit of code so I'm sharing it here for
everyone. Even though it doesn't really disable the button by greying
it out, it prevents the multiple submissions which it what I was
attempting to prevent all along. Disabling the button caused my
serverside events not to fire. I couldn't figure a way around that
limitation.

Put this in between the HEAD tags of your page:
<HEAD>
....
<SCRIPT language=javascript>
var submitFlag = false;
</SCRIPT>
....
</HEAD>

Add this line to Page_Load():
btn_RegisterMe.Attributes.Add("onclick",
"javascript:if(submitFlag){return false;}else{submitFlag=true;return
true;}")

Change btn_RegisterMe to whatever the name of your button that you
want to prevent multiple submissions from.

Chris
Nov 17 '05 #1
2 13142
Hello Chris,

Thanks very much for your post.

Best regards,
Yanhong Huang
Microsoft Online Partner Support

Get Secure! - www.microsoft.com/security
This posting is provided "AS IS" with no warranties, and confers no rights.

--------------------
!From: cr******@heery.com (techfuzz)
!Newsgroups: microsoft.public.dotnet.framework.aspnet
!Subject: disable or prevent a button with onclick and javascript
!Date: 30 Jul 2003 06:54:55 -0700
!Organization: http://groups.google.com/
!Lines: 28
!Message-ID: <63*************************@posting.google.com>
!NNTP-Posting-Host: 67.96.192.158
!Content-Type: text/plain; charset=ISO-8859-1
!Content-Transfer-Encoding: 8bit
!X-Trace: posting.google.com 1059573296 26385 127.0.0.1 (30 Jul 2003 13:54:56 GMT)
!X-Complaints-To: gr**********@google.com
!NNTP-Posting-Date: 30 Jul 2003 13:54:56 GMT
!Path: cpmsftngxa06.phx.gbl!TK2MSFTNGP08.phx.gbl!newsfeed 00.sul.t-online.de!t-online.de!newsfeed.freenet.de!
feed.news.nacamar.de!news.maxwell.syr.edu!sn-xit-03!sn-xit-01!sn-xit-09!supernews.com!postnews1.google.com!not-for-mail
!Xref: cpmsftngxa06.phx.gbl microsoft.public.dotnet.framework.aspnet:163359
!X-Tomcat-NG: microsoft.public.dotnet.framework.aspnet
!
!I scoured this group and others looking for the best way to disable a
!button after the first click to prevent multiple submissions, but
!never did find anything that worked like they said it would. I went
!ahead and wrote my own bit of code so I'm sharing it here for
!everyone. Even though it doesn't really disable the button by greying
!it out, it prevents the multiple submissions which it what I was
!attempting to prevent all along. Disabling the button caused my
!serverside events not to fire. I couldn't figure a way around that
!limitation.
!
!Put this in between the HEAD tags of your page:
!<HEAD>
!...
! <SCRIPT language=javascript>
! var submitFlag = false;
! </SCRIPT>
!...
!</HEAD>
!
!Add this line to Page_Load():
!btn_RegisterMe.Attributes.Add("onclick",
!"javascript:if(submitFlag){return false;}else{submitFlag=true;return
!true;}")
!
!Change btn_RegisterMe to whatever the name of your button that you
!want to prevent multiple submissions from.
!
!Chris
!
Nov 17 '05 #2
Hello Chris,

I was thinking about your excellent code sample. There may be some people who want to combine it with the use of
validation controls. Here is another code snippet that adds two elements to your code sample.

First, it only disables the button if validation succeeds. This allows the end user to try again if validation fails.

Second, it uses a hidden field allow the button to continue as disabled even after the post back to the server is complete.
Most people will not want this. Just remove the hidden field and the references to it. I added it to be thorough.
<script language="javascript" id="clientEventHandlersJS">
<!--

function Button1_OnClick() {
document.all("Button1").disabled = "disabled";
var AllowPost;
if (typeof(Page_ClientValidate) == 'function')
{
Page_ClientValidate();
if (Page_IsValid)
AllowPost = true;
else
AllowPost = false;
}
else
{
AllowPost = true;
}
if (AllowPost)
{
document.all("ButtonTest").value = "Button1";
Form1.submit();
return true;
}
else
{
document.all("Button1").disabled = "";
return false;
}
}
//-->
</script>
</HEAD>
<body>
<form id="Form1" method="post" runat="server">
<asp:textbox id="TextBox1" runat="server"></asp:textbox>
<asp:requiredfieldvalidator id="RequiredFieldValidator1" runat="server"
ErrorMessage="RequiredFieldValidator"
ControlToValidate="TextBox1"></asp:requiredfieldvalidator>
<asp:button id="Button1" runat="server" Text="Button"></asp:button>
<INPUT runat="server" id="ButtonTest" type="hidden" value="a"></form>
</body>
-----
Private Sub Page_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
Button1.CausesValidation = False
Button1.Attributes.Add("language", "javascript")
Button1.Attributes.Add("onclick", "Button1_OnClick();")
If ButtonTest.Value = "Button1" Then
Button1.Enabled = False
End If
End Sub

Thanks very much.

Best regards,
Yanhong Huang
Microsoft Online Partner Support

Get Secure! - www.microsoft.com/security
This posting is provided "AS IS" with no warranties, and confers no rights.

--------------------
!X-Tomcat-ID: 71586656
!References: <63*************************@posting.google.com>
!MIME-Version: 1.0
!Content-Type: text/plain
!Content-Transfer-Encoding: 7bit
!From: yh*****@online.microsoft.com (Yan-Hong Huang[MSFT])
!Organization: Microsoft
!Date: Fri, 01 Aug 2003 03:04:16 GMT
!Subject: RE: disable or prevent a button with onclick and javascript
!X-Tomcat-NG: microsoft.public.dotnet.framework.aspnet
!Message-ID: <Al**************@cpmsftngxa06.phx.gbl>
!Newsgroups: microsoft.public.dotnet.framework.aspnet
!Lines: 57
!Path: cpmsftngxa06.phx.gbl
!Xref: cpmsftngxa06.phx.gbl microsoft.public.dotnet.framework.aspnet:164013
!NNTP-Posting-Host: TOMCATIMPORT1 10.201.218.122
!
!Hello Chris,
!
!Thanks very much for your post.
!
!Best regards,
!Yanhong Huang
!Microsoft Online Partner Support
!
!Get Secure! - www.microsoft.com/security
!This posting is provided "AS IS" with no warranties, and confers no rights.
!
!--------------------
!!From: cr******@heery.com (techfuzz)
!!Newsgroups: microsoft.public.dotnet.framework.aspnet
!!Subject: disable or prevent a button with onclick and javascript
!!Date: 30 Jul 2003 06:54:55 -0700
!!Organization: http://groups.google.com/
!!Lines: 28
!!Message-ID: <63*************************@posting.google.com>
!!NNTP-Posting-Host: 67.96.192.158
!!Content-Type: text/plain; charset=ISO-8859-1
!!Content-Transfer-Encoding: 8bit
!!X-Trace: posting.google.com 1059573296 26385 127.0.0.1 (30 Jul 2003 13:54:56 GMT)
!!X-Complaints-To: gr**********@google.com
!!NNTP-Posting-Date: 30 Jul 2003 13:54:56 GMT
!!Path: cpmsftngxa06.phx.gbl!TK2MSFTNGP08.phx.gbl!newsfeed 00.sul.t-online.de!t-online.de!newsfeed.freenet.de!
!feed.news.nacamar.de!news.maxwell.syr.edu!sn-xit-03!sn-xit-01!sn-xit-09!supernews.com!postnews1.google.com!not-for-
mail
!!Xref: cpmsftngxa06.phx.gbl microsoft.public.dotnet.framework.aspnet:163359
!!X-Tomcat-NG: microsoft.public.dotnet.framework.aspnet
!!
!!I scoured this group and others looking for the best way to disable a
!!button after the first click to prevent multiple submissions, but
!!never did find anything that worked like they said it would. I went
!!ahead and wrote my own bit of code so I'm sharing it here for
!!everyone. Even though it doesn't really disable the button by greying
!!it out, it prevents the multiple submissions which it what I was
!!attempting to prevent all along. Disabling the button caused my
!!serverside events not to fire. I couldn't figure a way around that
!!limitation.
!!
!!Put this in between the HEAD tags of your page:
!!<HEAD>
!!...
!! <SCRIPT language=javascript>
!! var submitFlag = false;
!! </SCRIPT>
!!...
!!</HEAD>
!!
!!Add this line to Page_Load():
!!btn_RegisterMe.Attributes.Add("onclick",
!!"javascript:if(submitFlag){return false;}else{submitFlag=true;return
!!true;}")
!!
!!Change btn_RegisterMe to whatever the name of your button that you
!!want to prevent multiple submissions from.
!!
!!Chris
!!
!
!
!
Nov 17 '05 #3

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

4
by: @sh | last post by:
Can anyone help out here please, we have a button that when pressed will alert the user, should they cancel no action is taken, however should they confirm, the script will disable the button and...
1
by: Tee | last post by:
Hi, I have a server side Button that with the code Button3.Attributes.Add("onclick", "javascript:document.all.disabled=True; return false;") but it don't work as it supposed to be, disable...
2
by: Ghafran Abbas | last post by:
Call this function from the Page_OnLoad event of your asp.net page. This will prevent the user from doing multiple post backs or button clicks. This was designed to not disable the button, because...
3
by: Jeff | last post by:
I have a payment form with a submit button. A large percentage of users double-click the submit button thus submitting their payment information twice. I would like to use javascript to disable...
2
by: blarfoc | last post by:
Hi, I have to disable a button on a aspx page after the user clicks it. I have to disable the button with javascript because the process takes 20 seconds to run the full course. I kno I need to...
5
by: | last post by:
Hi all, Has anyone been able to write some custom javascript on the onclick event of submit button to do certain things like disable submit button, only submit form once etc. This was a breeze...
16
by: Barry Gilmore | last post by:
Is there a way to disable a button after it is clicked? I am trying to avoid having someone click on it twice while they wait for it to process. Thank you!
8
by: prado | last post by:
I want to disable a table with javascript. In this table i have 'n' record and each record has 3 buttons. If you click a button does an action. I want to disable the all table. is there any way...
9
by: poml | last post by:
Hello, first time posting on thescripts.com, and I'm in dire need of some help. All I want to do is disable the submit button (not the entire form) onClick, and am wondering if this is possible. ...
7
gskoli
by: gskoli | last post by:
Dear all, Let me tell you the scenario , i have called javascript function on radio button selection , Ex. Suppose There are 3 Radio Button . Let us consider i have clicked on one radio...
0
by: Naresh1 | last post by:
What is WebLogic Admin Training? WebLogic Admin Training is a specialized program designed to equip individuals with the skills and knowledge required to effectively administer and manage Oracle...
0
by: antdb | last post by:
Ⅰ. Advantage of AntDB: hyper-convergence + streaming processing engine In the overall architecture, a new "hyper-convergence" concept was proposed, which integrated multiple engines and...
1
by: Matthew3360 | last post by:
Hi there. I have been struggling to find out how to use a variable as my location in my header redirect function. Here is my code. header("Location:".$urlback); Is this the right layout the...
2
by: Matthew3360 | last post by:
Hi, I have a python app that i want to be able to get variables from a php page on my webserver. My python app is on my computer. How would I make it so the python app could use a http request to get...
0
by: AndyPSV | last post by:
HOW CAN I CREATE AN AI with an .executable file that would suck all files in the folder and on my computerHOW CAN I CREATE AN AI with an .executable file that would suck all files in the folder and...
0
by: Arjunsri | last post by:
I have a Redshift database that I need to use as an import data source. I have configured the DSN connection using the server, port, database, and credentials and received a successful connection...
1
by: Matthew3360 | last post by:
Hi, I have been trying to connect to a local host using php curl. But I am finding it hard to do this. I am doing the curl get request from my web server and have made sure to enable curl. I get a...
0
BLUEPANDA
by: BLUEPANDA | last post by:
At BluePanda Dev, we're passionate about building high-quality software and sharing our knowledge with the community. That's why we've created a SaaS starter kit that's not only easy to use but also...
0
by: Rahul1995seven | last post by:
Introduction: In the realm of programming languages, Python has emerged as a powerhouse. With its simplicity, versatility, and robustness, Python has gained popularity among beginners and experts...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.