2013-12-18 48 views
0

我試圖禁用按鈕並在回發時更改其文本,然後我想在回發完成時將文本更改回正常。在asp.net中回發期間禁用和啓用按鈕

<asp:Button ID="b_Generate" runat="server" onclick="b_Generate_Click" OnClientClick="this.disabled = true; this.value = 'Please Wait...';" 
Text="Generate PPT" UseSubmitBehavior="false" 
style="z-index: 1; left: 05px; top: 50px; position: absolute; width: 110px;" 
BorderStyle="Ridge" Font-Bold="True" 
ToolTip="click here" /> 

與上面的代碼我能夠禁用按鈕,改變後回到啓動前的文本,但我不知道如何再次啓用該按鈕並更改文本回以前一個曾經發回完成。

+0

通常情況下,由於該頁面會再次提交給客戶端,因此按鈕應在回發後獲取舊值。服務器甚至沒有提到客戶已經改變了它。 –

回答

3

我通常會將此方法添加到我的BasePage以供重用。您的代碼無法處理驗證

/// <summary> 
/// Avoid multiple submit 
/// </summary> 
/// <param name="button">The button.</param> 
/// <param name="text">text displayed on button</param> 
public void AvoidMultipleSubmit(Button button,string text="wait...") 
{ 
    Page.ClientScript.RegisterOnSubmitStatement(GetType(), "ServerForm", "if(this.submitted) return false; this.submitted = true;"); 
    button.Attributes.Add("onclick", string.Format("if(typeof(Page_ClientValidate)=='function' && !Page_ClientValidate()){{return true;}}this.value='{1}';this.disabled=true;{0}", 
     ClientScript.GetPostBackEventReference(button, string.Empty),text)); 
} 
+0

這會導致IE中的自動完成功能出現故障! –

+0

@ PaN1C_Showt1Me此代碼僅在回發期間運行...爲什麼在回發期間需要自動完成? – giammin

+0

不在回發期間。之後,自動填充功能不再有效。 –

1

您忘記了return true

<asp:Button ID="b_Generate" runat="server" onclick="b_Generate_Click" 
      OnClientClick="this.disabled = true; this.value = 'Please Wait...'; return true;" 
      Text="Generate PPT" UseSubmitBehavior="false" 
      style="z-index: 1; left: 05px; top: 50px; position: absolute; width: 110px;" 
      BorderStyle="Ridge" Font-Bold="True" 
      ToolTip="click here" 
/> 
+0

嗨嗨感謝您的回答,但是一旦我添加返回爲true,按鈕不會調用b_Generate_Click onclick方法。 – user2992534