Reputation: 71
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>
Upvotes: 3
Views: 11231
Reputation: 11
// add this code for global into layout or use for exact page
$('form').submit(function () {
$(':submit').unbind().click();
});
Upvotes: 1
Reputation: 911
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.
Upvotes: 3
Reputation: 11
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.
Upvotes: 1
Reputation: 128
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" />
Upvotes: 0