Prevent double clicking asp.net button

2020-05-31 03:44发布

I realise this question has been asked but none of the answers worked for my project.

I have a button that when clicked calls an API, so there is a 1 second delay.

I have tried several things nothing works.

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

Even that does nothing.

8条回答
Summer. ? 凉城
2楼-- · 2020-05-31 04:19

This is the one I found works in all cases.

<form id="form1" runat="server">
<asp:Button ID="Button1" runat="server" Text="Button1" OnClick="Button1_Clicked" />
<asp:Button ID="Button2" runat="server" Text="Button2" />
</form>

Now here’s the short JavaScript snippet that will disable the button as soon as it is clicked so that when PostBack occurs the button cannot be clicked again.
<script type = "text/javascript">
function DisableButton() {
    document.getElementById("<%=Button1.ClientID %>").disabled = true;
}
window.onbeforeunload = DisableButton;
</script>

The above script disables the ASP.Net Button as soon as the page is ready to do a PostBack or the ASP.Net form is submitted.
But in cases you might want to disable all Buttons and Submit Buttons on the page hence for such cases I have created another function which disables all Buttons and Submit buttons whenever there’s a PostBack or form submission
<script type = "text/javascript">
function DisableButtons() {
    var inputs = document.getElementsByTagName("INPUT");
    for (var i in inputs) {
        if (inputs[i].type == "button" || inputs[i].type == "submit") {
            inputs[i].disabled = true;
        }
    }
}
window.onbeforeunload = DisableButtons;
</script>
查看更多
做个烂人
3楼-- · 2020-05-31 04:32

Prevent Double Click .Please add below code in your aspx page.

<script type="text/javascript" language="javascript">

   Sys.WebForms.PageRequestManager.getInstance().add_beginRequest(BeginRequestHandler);
   function BeginRequestHandler(sender, args) { var oControl = args.get_postBackElement(); oControl.disabled = true; }

</script>
查看更多
【Aperson】
4楼-- · 2020-05-31 04:33

Prevent Double Click .Please add below code in your aspx page

<script type = "text/javascript">
function DisableButton() {
document.getElementById("<%=Button1.ClientID %>").disabled = true;
}
window.onbeforeunload = DisableButton;
</script>
查看更多
Root(大扎)
5楼-- · 2020-05-31 04:36

This solution is simple and effective. On your button include this code:

OnClientClick="return CheckDouble();"

And wherever you want your JavaScript - e.g. At the bottom of your page:

<script type="text/javascript">
   var submit = 0;
   function CheckDouble() {
     if (++submit > 1) {
     alert('This sometimes takes a few seconds - please be patient.');
     return false;
   }
 }
 </script>
查看更多
Emotional °昔
6楼-- · 2020-05-31 04:36

You can prevent double-clicking using this code:

Me.btnSave.Attributes.Add("onclick", "this.disabled=true;")
Me.btnSave.UseSubmitBehavior = False

So you can use btnSave_Click to call your API.

Usually I have a lot of Validators in my Page: setting Validator.SetFocusOnError = True I can run this code to reenable save button if a validation failed.

Me.YourControl.Attributes.Add("onfocus", Me.btnSave.ClientID & ".removeAttribute('disabled');")
查看更多
ら.Afraid
7楼-- · 2020-05-31 04:38

Most of the above suggestions failed to work for me. The one that did work was the following by tezzo:

Me.btnSave.Attributes.Add("onclick", "this.disabled=true;")
Me.btnSave.UseSubmitBehavior = False

Simpler still, rather than using the above in the code-behind, just use the following:

   <asp:Button ID="btnSave" runat="server" Text="Save" 
      UseSubmitBehavior="false"
      OnClientClick="this.disabled='true';" 
   </asp:button>

UseSubmitBehavior="false" is the key.

查看更多
登录 后发表回答