在asp.net中回发期间禁用和启用按钮

时间:2013-12-18 10:36:56

标签: c# javascript 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" />

使用上面的代码,我可以在回发开始之前禁用按钮并更改文本但我不知道如何再次启用按钮并在回发完成后将文本更改回上一个

2 个答案:

答案 0 :(得分:4)

我通常会将此方法添加到我的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));
}

答案 1 :(得分: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"
/>