Disable button to prevent double clicking

Discussion in 'ASP .Net' started by Buddy Ackerman, Feb 27, 2006.

  1. 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
     
    Buddy Ackerman, Feb 27, 2006
    #1
    1. Advertising

  2. Buddy Ackerman

    Parag Guest

    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



    "Buddy Ackerman" <> wrote in message
    news:...
    > 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
    >
    >
     
    Parag, Feb 27, 2006
    #2
    1. Advertising

  3. Post you client-side event handler.

    Eliyahu

    "Buddy Ackerman" <> wrote in message
    news:...
    > 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
    >
    >
     
    Eliyahu Goldin, Feb 27, 2006
    #3
  4. 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
    "Buddy Ackerman" <> wrote in message
    news:...
    > 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
    >
    >
     
    S. Justin Gengo [MCP], Feb 27, 2006
    #4
  5. <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
     
    blackstaronline.net, Feb 28, 2006
    #5
  6. Buddy Ackerman

    JavaJoe

    Joined:
    Nov 3, 2006
    Messages:
    1
    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
     
    JavaJoe, Nov 3, 2006
    #6
  7. Buddy Ackerman

    Adam Anderson

    Joined:
    Nov 7, 2007
    Messages:
    1
    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.
     
    Adam Anderson, Nov 7, 2007
    #7
  8. Buddy Ackerman

    sobolewski.jakub

    Joined:
    Jan 8, 2008
    Messages:
    1
    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
     
    sobolewski.jakub, Jan 8, 2008
    #8
  9. Buddy Ackerman

    Steve_L

    Joined:
    Jan 11, 2008
    Messages:
    1
    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
     
    Steve_L, Jan 11, 2008
    #9
  10. Buddy Ackerman

    CautiousOldGuy

    Joined:
    Oct 15, 2009
    Messages:
    1
    Steve_L's solution works for me. Neat too.
     
    CautiousOldGuy, Oct 15, 2009
    #10
  11. Buddy Ackerman

    Joined:
    Sep 24, 2010
    Messages:
    1
    This one will work :

    <asp:Button id="submitButton" runat="server" Text="Submit" UseSubmitBehavior="false" OnClientClick="this.disabled=true; this.value = 'Please wait...';" />
     
    , Sep 24, 2010
    #11
    1. Advertising

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

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. techfuzz
    Replies:
    2
    Views:
    673
    Yan-Hong Huang[MSFT]
    Aug 4, 2003
  2. James Brett
    Replies:
    4
    Views:
    11,846
    James Brett
    Nov 26, 2003
  3. Sydex
    Replies:
    12
    Views:
    6,647
    Victor Bazarov
    Feb 17, 2005
  4. Flyguy

    Double clicking a web page button

    Flyguy, Jan 16, 2009, in forum: ASP .Net
    Replies:
    5
    Views:
    519
    James BT
    Jan 27, 2009
  5. Replies:
    2
    Views:
    208
Loading...

Share This Page