473,566 Members | 3,040 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

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=javasc ript>
var submitFlag = false;
</SCRIPT>
....
</HEAD>

Add this line to Page_Load():
btn_RegisterMe. Attributes.Add( "onclick",
"javascript:if( submitFlag){ret urn false;}else{sub mitFlag=true;re turn
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 13253
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.publi c.dotnet.framew ork.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************ *************@p osting.google.c om>
!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**********@go ogle.com
!NNTP-Posting-Date: 30 Jul 2003 13:54:56 GMT
!Path: cpmsftngxa06.ph x.gbl!TK2MSFTNG P08.phx.gbl!new sfeed00.sul.t-online.de!t-online.de!newsf eed.freenet.de!
feed.news.nacam ar.de!news.maxw ell.syr.edu!sn-xit-03!sn-xit-01!sn-xit-09!supernews.co m!postnews1.goo gle.com!not-for-mail
!Xref: cpmsftngxa06.ph x.gbl microsoft.publi c.dotnet.framew ork.aspnet:1633 59
!X-Tomcat-NG: microsoft.publi c.dotnet.framew ork.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=javasc ript>
! var submitFlag = false;
! </SCRIPT>
!...
!</HEAD>
!
!Add this line to Page_Load():
!btn_RegisterMe .Attributes.Add ("onclick",
!"javascript:if (submitFlag){re turn false;}else{sub mitFlag=true;re turn
!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="javas cript" id="clientEvent HandlersJS">
<!--

function Button1_OnClick () {
document.all("B utton1").disabl ed = "disabled";
var AllowPost;
if (typeof(Page_Cl ientValidate) == 'function')
{
Page_ClientVali date();
if (Page_IsValid)
AllowPost = true;
else
AllowPost = false;
}
else
{
AllowPost = true;
}
if (AllowPost)
{
document.all("B uttonTest").val ue = "Button1";
Form1.submit();
return true;
}
else
{
document.all("B utton1").disabl ed = "";
return false;
}
}
//-->
</script>
</HEAD>
<body>
<form id="Form1" method="post" runat="server">
<asp:textbox id="TextBox1" runat="server"> </asp:textbox>
<asp:requiredfi eldvalidator id="RequiredFie ldValidator1" runat="server"
ErrorMessage="R equiredFieldVal idator"
ControlToValida te="TextBox1"> </asp:requiredfie ldvalidator>
<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.EventArg s) Handles MyBase.Load
Button1.CausesV alidation = False
Button1.Attribu tes.Add("langua ge", "javascript ")
Button1.Attribu tes.Add("onclic k", "Button1_OnClic k();")
If ButtonTest.Valu e = "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************ *************@p osting.google.c om>
!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.publi c.dotnet.framew ork.aspnet
!Message-ID: <Al************ **@cpmsftngxa06 .phx.gbl>
!Newsgroups: microsoft.publi c.dotnet.framew ork.aspnet
!Lines: 57
!Path: cpmsftngxa06.ph x.gbl
!Xref: cpmsftngxa06.ph x.gbl microsoft.publi c.dotnet.framew ork.aspnet:1640 13
!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.publi c.dotnet.framew ork.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************ *************@p osting.google.c om>
!!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**********@go ogle.com
!!NNTP-Posting-Date: 30 Jul 2003 13:54:56 GMT
!!Path: cpmsftngxa06.ph x.gbl!TK2MSFTNG P08.phx.gbl!new sfeed00.sul.t-online.de!t-online.de!newsf eed.freenet.de!
!feed.news.naca mar.de!news.max well.syr.edu!sn-xit-03!sn-xit-01!sn-xit-09!supernews.co m!postnews1.goo gle.com!not-for-
mail
!!Xref: cpmsftngxa06.ph x.gbl microsoft.publi c.dotnet.framew ork.aspnet:1633 59
!!X-Tomcat-NG: microsoft.publi c.dotnet.framew ork.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=javasc ript>
!! var submitFlag = false;
!! </SCRIPT>
!!...
!!</HEAD>
!!
!!Add this line to Page_Load():
!!btn_RegisterM e.Attributes.Ad d("onclick",
!!"javascript:i f(submitFlag){r eturn false;}else{sub mitFlag=true;re turn
!!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
2841
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 then submit the form... Could someone correct this lump of code for us pleaseeeeeeee ;o) <input name="Submit" type="button"...
1
1865
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 button and the return false prevent it submit back to server.
2
8917
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 asp.net 1.x will not pick up the button click event. Instead, it monitors the on submit event of the form and the _doPostBack event of the asp.net...
3
3149
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 the submit button once it's been clicked, yet still have the form submit. I can do this in ASP 2.0, however, ASP.Net seems to be adversely affected...
2
2046
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 do this with javascript but I do not kno how. Please tell me if you will please.
5
2633
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 in 1.1 since I could edit the .js file. Now in 2.0 I can no longer do this. Also, my code would have to be called after all client-side validation...
16
3412
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
25184
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 to disable all this table without have to disable each button of each record? Thanks
9
7132
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. Here's the current coding: <form enctype="multipart/form-data" action="?id=completeupload" name="submitform" method="POST"> <b>File to...
7
3818
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 button then javascript is called on that and it get 'checked' But again if if i clicked on that radio , Then again it will call the javascript ....
0
7586
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 effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language...
1
7649
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 Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For...
0
7957
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 protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the...
0
6270
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then...
1
5487
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes...
0
5215
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert...
0
3647
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in...
0
3628
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
2092
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system

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.