2009-10-31 70 views
1

我需要點擊期間禁用按鈕,需要啓用我的服務器端事件完成 後,現在我的代碼看起來像這樣禁用和服務器事件後啓用的jQuery按鈕

<script src="../Scripts/jquery-1.3.2.js" type="text/javascript"></script> 
<script type ="text/javascript" > 


$(document).ready(function(){ 
    $('.saveButton').bind("click", function(e) { 
     $(this).attr("disabled", "true"); 
     return true; //causes the client side script to run. 
    }); 
}); 
</script> 
//this works finr for disableing the button 
//how to enable the button from disabled to enable after my server side event as executed 

<asp:Button ID="Button1" runat="server" CssClass="saveButton" Text="Button" 
     onclick="Button1_Click" /> 
<asp:Button ID="Button2" runat="server" CssClass="saveButton" Text="Button" 
      onclick="Button2_Click" /> 

//Server side events 
protected void Button1_Click(object sender, EventArgs e) 
{ 
    try 
    { 
    } 
    catch() 
    { 
    } 
    finally 
    { 
     //once my control reaches here i need to enable my save from disabled to enabled stae 
     // can we do this in jQuery 
    } 
} 

尋找這個從過去的2天代碼 在這個任何解決方案將是巨大的謝謝 謝謝

回答

3

您可以使用RegisterClientScriptBlock方法對你Button1_Click事件,從服務器端添加客戶端腳本:

var enableButton = "$('.saveButton').removeAttr('disabled');"; 
Page.ClientScript.RegisterClientScriptBlock(this.Page.GetType(), "EnableButton", 
              enableButton, true); 
0

我認爲這裏的核心問題是您對頁面生命週期的理解,您可能需要在此閱讀http://msdn.microsoft.com/en-us/library/ms178472.aspx

看起來好像你不一定想要回頁面。考慮使用jQuery ajax更新服務器,並在回調中重新啓用該按鈕。

或者,這將重新啓用5秒後的按鈕(回發後(希望如此)!)

$(document).ready(function() 
    { $('.saveButton').bind("click", function(e) { 
         $(this).attr("disabled", "true"); 
         setTimeout(function(){$(this).removeAttr("disabled");}, 5000); 
         return true; 
         }); 
    }); 

或者說,你的按鈕單擊事件結束寫入文字控制是這樣的: 「(document).ready(function(){$('jQueryButtonSelector')。removeAttr('disabled');});」

還是做使用相同的Response.Write()

+1

哎這裏發生了什麼是按鈕越來越細禁用一次我現在點擊button.but他並沒有叫我的服務器端event only ie protected void Button1_Click(object sender,EventArgs e) ///我的代碼 } – happysmile 2009-11-01 13:11:07

0

我遇到了與其他答案不是調用服務器端事件一旦停用的問題,我解決異步禁用按鈕。

客戶

<asp:Button runat="server" ID="submitButton" ValidationGroup="DynamicForm" OnClientClick="disableButton(this);" /> 
<script> 
    function disableButton(sender) { // Disable to prevent duplicate submission. Enabled in code behind 
     if (Page_ClientValidate("DynamicForm")) { 
      setTimeout(function() { $('#<%= submitButton.ClientID%>').attr("disabled", "true"); }, 0); // Asynchronously disable, otherwise disabled button won't submit first time 
     } 
    } 
</script> 

服務器

Private Sub submitbutton_Click(sender As Object, e As EventArgs) Handles submitButton.Click 
    ... 
    Page.ClientScript.RegisterClientScriptBlock(Page.GetType(), "EnableButton", "$('#" + submitButton.ClientID + "').removeAttr('disabled');", True) 
End Sub