disable or prevent a button with onclick and javascript

Ask a Question related to ASP.NET General, Design and Development.

  1. #1

    Default 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
    techfuzz Guest

  2. Similar Questions and Discussions

    1. Enable Disable fields onclick
      I have a cfinput field that is disabled when the form first loads (disabled="true") I want to click a button to enable the field. ...
    2. Using onClick Javascript in cfinput
      I hope someone out here can help me with this problem: I'm using Flash forms in CFMX7, and have a button (type="button") that I want to jump to a...
    3. button onclick
      I have a flash form with a button. When the button is clicked, I would like the "onClick()" function to forward the user to a different page that I...
    4. onClick event on a Submit button
      I have a form that has one input field and a submit button that has an onclick event coded that calles a javascript function. If focus is on the...
    5. JavaScript Onclick firing a button (C#)
      Has anybody ever used JavaScript to fire a button? I want a user to be able to click anywhere in a row to be able to make a selection. I use...
  3. #2

    Default RE: disable or prevent a button with onclick and javascript

    Hello Chris,

    Thanks very much for your post.

    Best regards,
    Yanhong Huang
    Microsoft Online Partner Support

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

    --------------------
    !From: [email]cruegseg@heery.com[/email] (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: [url]http://groups.google.com/[/url]
    !Lines: 28
    !Message-ID: <639a482f.0307300554.7b4127a@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: [email]groups-abuse@google.com[/email]
    !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
    !


    Yan-Hong Huang[MSFT] Guest

  4. #3

    Default RE: disable or prevent a button with onclick and javascript

    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! - [url]www.microsoft.com/security[/url]
    This posting is provided "AS IS" with no warranties, and confers no rights.

    --------------------
    !X-Tomcat-ID: 71586656
    !References: <639a482f.0307300554.7b4127a@posting.google.com>
    !MIME-Version: 1.0
    !Content-Type: text/plain
    !Content-Transfer-Encoding: 7bit
    !From: [email]yhhuang@online.microsoft.com[/email] (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: <Alopcm9VDHA.2080@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! - [url]www.microsoft.com/security[/url]
    !This posting is provided "AS IS" with no warranties, and confers no rights.
    !
    !--------------------
    !!From: [email]cruegseg@heery.com[/email] (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: [url]http://groups.google.com/[/url]
    !!Lines: 28
    !!Message-ID: <639a482f.0307300554.7b4127a@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: [email]groups-abuse@google.com[/email]
    !!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
    !!
    !
    !
    !


    Yan-Hong Huang[MSFT] Guest

Posting Permissions

  • You may not post new threads
  • You may post replies
  • You may not post attachments
  • You may not edit your posts

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139