473,395 Members | 1,956 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,395 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 13230
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: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...

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.