2011-03-04 140 views
31

我有一個ASP.NET按鈕,我需要禁用後,用戶單擊它,以防止雙擊。提交完成後,必須再次啓用。誰能幫我這個?點擊後禁用asp.net按鈕,以防止雙擊

+0

很簡單button.enable =所需的代碼完成後 – Dotnet 2011-03-04 04:21:34

+0

假這是一個正常的回傳或AJAX? – 2011-03-04 04:23:18

+0

如果您不想通過按鈕修復此問題,請參閱我的回答:http://stackoverflow.com/a/28844217/787757 – sparebytes 2015-03-05 15:27:20

回答

0

你可以用javascript來做到這一點。在您的form標記中,onsubmit="javascript:this.getElementById("submitButton").disabled='true';"

2

禁用OnClick事件上的按鈕,然後在AJAX回調事件處理程序上重新啓用。以下是我如何使用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> 
3

您可以使用客戶端onclick事件要做到這一點:

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

我發現this,和它的作品:

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

什麼是clientScript? – 2017-07-21 11:42:16

-2

沒有上述解決方案的工作我最終找到了最簡單的方法。

button1.enabled = false 

當你需要啓用只是做到這一點。

button1.enabled = true 
+6

這是如何防止雙擊?它只會在請求完成後才被禁用,在此期間可以點擊一堆。 – guanome 2012-06-01 14:23:35

1

使用jQuery:

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

檢查這個環節,最簡單的方法,它不會禁用驗證。

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

如果你有例如,

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

然後你可以使用

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

要禁用按鈕如果驗證成功,就像在頁面做一個服務器回傳。

+0

鏈接只有答案不歡迎堆棧溢出。 – hims056 2012-10-11 06:05:41

0

這適用於普通的html按鈕。

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

該按鈕被禁用,直到回發完成,防止雙擊。

21

這是一個適用於asp.net按鈕對象的解決方案。在前端,這些屬性添加到您的ASP:按鈕定義:

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

在後端,在點擊事件處理方法的調用,這個代碼添加到末尾(最好是在finally塊)

myButton.Enabled = true; 
+3

它不工作:( – 2014-04-14 16:59:31

1

我喜歡禁用按鈕並調用回發,這樣在禁用按鈕後,仍然可以運行後面的代碼。

這是我從代碼附加背後:

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

然後重新啓用背後的代碼按鈕:

btnProcess.Enabled = True 
1

試試這個,它爲我工作

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

您可以調整延遲時間「的setTimeout」

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

如果你想防止雙擊由於響應緩慢的服務器端,然後這個代碼工作正常:

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

試着把Threading.Thread.Sleep(5000)放在_Click()事件在服務器上,您將看到該按鈕在服務器處理單擊事件時被禁用。

無需服務器端代碼要麼重新啓用按鈕!

+0

什麼,我需要完成您還可以添加'this.style.backgroundcolor =「#abcdef」完美工作;。'來改變元素的風格方面 – 2017-04-25 20:42:04

+0

這工作完全只是想強調需要'UseSubmitBehavior =「false」',否則你的按鈕點擊不會在代碼隱藏中觸發。 – 2018-01-04 17:06:16

0

如果有人關心我發現這個職位最初,但我在網頁上使用ASP.NET的構建中驗證。解決方案能夠正常工作,但即使驗證了該按鈕,也可以禁用該按鈕。您可以使用下面的代碼來創建它,以便它只在頁面驗證通過時禁用該按鈕。

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

此解決方案,如果你正在使用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> 

和更改按鈕:

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