Disable asp.net button after click to prevent doub

2019-01-17 14:12发布

I have an ASP.NET button that I need to disable after the user clicks it to prevent double-clicking. Once the submit completes it has to be enabled again. Can anyone help me with this?

16条回答
老娘就宠你
2楼-- · 2019-01-17 14:39

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
查看更多
Evening l夕情丶
3楼-- · 2019-01-17 14:41

Disable the button on the OnClick event, then re-enable on the AJAX callback event handler. Here is how I do it with 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>
查看更多
做个烂人
4楼-- · 2019-01-17 14:42

I have found this, and it works:

btnSave.Attributes.Add(
    "onclick", 
    "this.disabled = true;" + ClientScript.GetPostBackEventReference(btnSave, null) + ";");
查看更多
太酷不给撩
5楼-- · 2019-01-17 14:44

Just need to add 2 attributes for asp button :

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

e.g.

<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

查看更多
手持菜刀,她持情操
6楼-- · 2019-01-17 14:44

You can do this with javascript. in your form tag, onsubmit="javascript:this.getElementById("submitButton").disabled='true';"

查看更多
爷的心禁止访问
7楼-- · 2019-01-17 14:48

You can use the client-side onclick event to do that:

yourButton.Attributes.Add("onclick", "this.disabled=true;");
查看更多
登录 后发表回答