3

How can I prevent the user from double-clicking a button? I have been searching the internet and nothing works for me in my project. Can you give me examples?

I've this and doesn't work

 <script type = "text/javascript">
 function DisableButton() {
  document.getElementById("<%=Button1.ClientID %>").disabled = true;
 }
  window.onbeforeunload = DisableButton;
 </script>
MichaelPth
  • 71
  • 3
  • 9

4 Answers4

3

Try this

<asp:Button OnClientClick="disableClick(this)" OnClick="MyButtonOnClick" runat="server" UseSubmitBehavior="false" ID="MyButton" />

<script type="text/javascript">
    function disableClick(elem) {
        elem.disabled = true;
    }
</script>

You hook into the OnClientClick event and you pass in the button to the disableClick function and then the button is set to disabled so it can't be clicked again.

KSib
  • 893
  • 1
  • 7
  • 24
  • I need to remove the OnClick from the asp label of the button because I saw that you do not have that event, I understood that it is just adding OnClientClick, if I only add that button, it will not send another form, in a nutshell will not the button do anything? That happens to me in my project – MichaelPth Jan 17 '17 at 18:58
  • You can still use the OnClick event because the button needs to post back initially. I don't know how you are using the button because I don't see the button code posted, but you may need to do what I've just edited the code to do for it to work effectively. Just a sec. – KSib Jan 17 '17 at 19:08
  • This solution usually does not work. "elem.disabled" immediately cancels the current postback - except you change the submit behavior. See https://stackoverflow.com/a/21996413/2534462 – Fried Dec 22 '17 at 08:25
1

None of the other answers really helped me but on applying the code from the link that Junior Porfirio shared (http://tgynther.blogspot.com.br/2011/07/aspnet-prevent-button-double-click.html) under the comments is what finally gave me the solution I was looking for.

Button1.Attributes.Add("onclick", " this.disabled = true; " + ClientScript.GetPostBackEventReference(Button1, null) + ";");

I added the above code in the Page_Load method and it seems like its working. The button is disabled and the postback still takes place.

Manju M
  • 11
  • 3
1
    // add this code for global into layout or use for exact page
    $('form').submit(function () {
        $(':submit').unbind().click();

    });
nilkin
  • 11
  • 2
0

You can change the 'enabled' property to false in the click event of the button.

e.g:

On the c# code:

protected void Button1_Click(object sender, EventArgs e)
{
    Button1.Enabled = false;
}

On the aspx file:

<asp:Button ID="Button1" runat="server" Text="Button" OnClick="Button1_Click" />
Avivbuen
  • 128
  • 9
  • 1
    This won't always prevent a double click unless the server side code is incredibly fast before someone has a chance to click it again. This is relying on the server to disable the button. – KSib Jan 16 '17 at 21:35
  • In case he does it on the client side the server side won't remember the state after a second post. it is possible to store a variable on the session but I think it's an overkill, it does not matter if you click one time or more before the postback because the event will execute one time any way. after it will be executed you will not be able to do the same task again. – Avivbuen Jan 16 '17 at 22:02
  • You can certainly do it twice before the postback. That's what I'm getting at. Have a long-running process before you get to the `Button1.Enabled = false` and it will run the code two times. This current example will work most of the time because there's barely anything going on. – KSib Jan 16 '17 at 22:04
  • If he wants to prevent the button click after the method posts back and returns data to the user then he needs your answer in addition to a javascript solution so this answer can still be valid if that's what Marth needs. – KSib Jan 16 '17 at 22:06
  • Then you can save a cookie that stores the button state and use js to store and apply it, or use a bool that prevents a second run of the event. – Avivbuen Jan 16 '17 at 22:13
  • 1
    That's doing too much now if you're wanting to bring cookies into this. What you have plus the Javascript solution below should be fine unless his or her requirements aren't what we think they are. – KSib Jan 16 '17 at 22:16