【问题标题】:Disabling and enabling a button during postback in asp.net [duplicate]在asp.net中的回发期间禁用和启用按钮[重复]
【发布时间】:2014-01-06 11:49:33
【问题描述】:

我正在尝试禁用按钮并在回发时更改其文本,然后我想在回发完成后将文本更改回正常状态。

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

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

【问题讨论】:

  • 由于页面再次提交给客户端,通常按钮应该在回发后获得它的旧值。服务端甚至没有提到客户端改变了它。

标签: c# javascript asp.net


【解决方案1】:

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

【讨论】:

  • 这会导致 IE 中的自动完成功能出现故障!
  • @PaN1C_Showt1Me 此代码仅在回发期间运行...为什么在回发期间需要自动完成??
  • 不是在回发期间。之后,自动完成功能不再起作用。
  • @PaN1C_Showt1Me 浏览器自动完成或外部自动完成插件
  • 浏览器自动完成。默认来自 IE
【解决方案2】:

你忘了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"
/>

【讨论】:

  • 嗨,嗨,谢谢您的回答,但是一旦我将 return 添加为 true,按钮就不会调用 b_Generate_Click onclick 方法。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2012-05-26
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2015-06-20
  • 2014-09-27
相关资源
最近更新 更多