简体   繁体   English

单击后禁用asp.net按钮以防止双击

[英]Disable asp.net button after click to prevent double clicking

I have an ASP.NET button that I need to disable after the user clicks it to prevent double-clicking. 我有一个ASP.NET按钮,我需要在用户单击它后禁用它以防止双击。 Once the submit completes it has to be enabled again. 提交完成后,必须再次启用它。 Can anyone help me with this? 谁能帮我这个?

Here is a solution that works for the asp.net button object. 这是一个适用于asp.net按钮对象的解决方案。 On the front end, add these attributes to your asp:Button definition: 在前端,将这些属性添加到您的asp:Button定义:

<asp:Button ... OnClientClick="this.disabled=true;" UseSubmitBehavior="false" />

In the back end, in the click event handler method call, add this code to the end (preferably in a finally block) 在后端,在click事件处理程序方法调用中,将此代码添加到末尾(最好在finally块中)

myButton.Enabled = true;

I have found this , and it works: 我找到了这个 ,它有效:

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

Check this link, the most simplest way and it does not disable the validations. 检查此链接,这是最简单的方法,它不会禁用验证。

http://aspsnippets.com/Articles/Disable-Button-before-Page-PostBack-in-ASP.Net.aspx http://aspsnippets.com/Articles/Disable-Button-before-Page-PostBack-in-ASP.Net.aspx

If you have eg 如果你有例如

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

Then you can use 然后你可以使用

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

To disable the button if and after validation has succeeded, just as the page is doing a server postback. 要禁用按钮,如果 验证成功,就像在页面做一个服务器回传。

If you want to prevent double clicking due to a slow responding server side code then this works fine: 如果你想防止由于响应缓慢的服务器端代码双击,那么这工作正常:

<asp:Button ... OnClientClick="this.disabled=true;" UseSubmitBehavior="false" />

Try putting a Threading.Thread.Sleep(5000) on the _Click() event on the server and you will see the button is disabled for the time that the server is processing the click event. 尝试在服务器上的_Click()事件上放置Threading.Thread.Sleep(5000),您将看到该按钮在服务器处理click事件时被禁用。

No need for server side code to re-enable the button either! 无需服务器端代码重新启用按钮!

<asp:Button ID="btnSend" runat="server" Text="Submit"  OnClick="Button_Click"/>

        <script type = "text/javascript">
            function DisableButton()
            {
                document.getElementById("<%=btnSend.ClientID %>").disabled = true;
            }
            window.onbeforeunload = DisableButton;
        </script>

If anyone cares I found this post initially, but I use ASP.NET's build in Validation on the page. 如果有人关心我最初发现这篇文章,但我在页面上使用ASP.NET的验证版本。 The solutions work, but disable the button even if its been validated. 解决方案有效,但即使已经过验证,也会禁用该按钮。 You can use this following code in order to make it so it only disables the button if it passes page validation. 您可以使用以下代码,以便只有在通过页面验证时才会禁用该按钮。

<asp:Button ID="Button1" runat="server" onclick="Button1_Click" Text="Submit" OnClientClick=" if ( Page_ClientValidate() ) { this.value='Submitting..'; this.disabled=true; }" UseSubmitBehavior="false" />

Just need to add 2 attributes for asp button : 只需要为asp按钮添加2个属性:

OnClientClick="this.disabled='true';"  UseSubmitBehavior="false"

eg 例如

<asp:Button ID="Button1" runat="server" Text="TEST" OnClientClick="this.disabled='true';"  UseSubmitBehavior="false" CssClass="button-content btnwidth" OnClick="ServerSideMethod_Click"  />

For more details: 更多细节:

https://bytes.com/topic/asp-net/answers/918280-disable-button-click-prevent-multiple-postbacks https://bytes.com/topic/asp-net/answers/918280-disable-button-click-prevent-multiple-postbacks

您可以使用客户端onclick事件来执行此操作:

yourButton.Attributes.Add("onclick", "this.disabled=true;");

Disable the button on the OnClick event, then re-enable on the AJAX callback event handler. 禁用OnClick事件上的按钮,然后在AJAX回调事件处理程序上重新启用。 Here is how I do it with jQuery. 以下是我使用jQuery的方法。

<script>
$(document).ready(function() {

    $('#buttonId').click(function() {
         $(this).attr('disabled', 'disabled');
         callAjax();
    });

});

function callAjax()
{
    $.ajax({
      url: 'ajax/test.html',
      success: function(data) {
         //enable button
         $('#buttonId').removeAttr('disabled');

      }
    });
}
</script>

using jQuery: 使用jQuery:

$('form').submit(function(){
    $(':submit', this).click(function() {
        return false;
    });
});

I like to disable the button and call a postback, this way the code behind still gets run after the button is disabled. 我喜欢禁用按钮并调用回发,这样后面的代码仍然会在按钮被禁用后运行。

this is how i attach from code behind: 这是我从代码背后附加的方式:

btnProcess.Attributes.Add("onclick", " this.disabled = true; __doPostBack('btnProcess', ''); return false;")

Then re-enable the button in code behind: 然后在代码后面重新启用按钮:

btnProcess.Enabled = True

Try this, it worked for me 试试这个,它对我有用

<asp:Button ID="button" runat="server" CssClass="normalButton" 
Text="Button"  OnClick="Button_Click" ClientIDMode="Static" 
OnClientClick="this.disabled = true; setTimeout('enableButton()', 1500);" 
UseSubmitBehavior="False"/>  

Then 然后

<script type="text/javascript">
function enableButton() {
document.getElementById('button').disabled = false;
}
</script>

You can adjust the delay time in "setTimeout" 您可以在“setTimeout”中调整延迟时间

This solution works if you are using asp.net validators: 如果您使用的是asp.net验证器,此解决方案将起作用:

<script language="javascript" type="text/javascript">
function disableButton(sender,group)
{
    Page_ClientValidate(group);
    if (Page_IsValid)
    {
        sender.disabled = "disabled";
        __doPostBack(sender.name, '');
    }
}</script>

and change the button: 并更改按钮:

<asp:Button runat="server" ID="btnSendMessage" Text="Send" OnClick="btnSendMessage_OnClick" OnClientClick="disableButton(this,'theValidationGroup')" CausesValidation="true" ValidationGroup="theValidationGroup" />

This works with a regular html button. 这适用于常规的html按钮。

    <input id="Button1"
    onclick="this.disabled='true';" type="button"
    value="Submit" name="Button1"
    runat="server" onserverclick="Button1_Click">

The button is disabled until the postback is finished, preventing double clicking. 该按钮被禁用,直到回发结束,防止双击。

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

You can do this with javascript. 你可以用javascript做到这一点。 in your form tag, onsubmit="javascript:this.getElementById("submitButton").disabled='true';" 在你的form标签中, onsubmit="javascript:this.getElementById("submitButton").disabled='true';"

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM