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>
4
  • What do you mean by double clicking? To prevent the user two click twice a submit button? Commented Jan 16, 2017 at 19:08
  • You can try it: tgynther.blogspot.com.br/2011/07/… Commented Jan 16, 2017 at 19:12
  • 2
    Why don't you write "Button1.enabled = false" in Button1 click event instead of a javascript function..? Commented Jan 16, 2017 at 19:28
  • this has been answered in stackoverflow.com/a/11832053/1863856 Commented Dec 28, 2017 at 12:14

4 Answers 4

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.

Sign up to request clarification or add additional context in comments.

3 Comments

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
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.
This solution usually does not work. "elem.disabled" immediately cancels the current postback - except you change the submit behavior. See stackoverflow.com/a/21996413/2534462
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.

3 Comments

Please use proper formatting, put four space before Button1, and remove the backticks: `
Done. Thanks SteveFest.
how to enable the button after response?
1
    // add this code for global into layout or use for exact page
    $('form').submit(function () {
        $(':submit').unbind().click();

    });

Comments

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" />

6 Comments

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.
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.
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.
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.
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.
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.