【问题标题】:Disable a button when clicked then enable after refresh单击时禁用按钮,然后在刷新后启用
【发布时间】:2013-06-18 09:45:43
【问题描述】:

我在页面上有一个按钮,单击该按钮会运行一个子组件,该子组件会使用 SQL 表中的数据填充网格视图。我正在使用 VS2010 和 VB。

当用户单击提交按钮时,我想立即禁用它,然后运行子程序,一旦子程序运行,再次启用按钮。

我在按钮单击事件中添加了以下内容,但它不起作用

        imgbtnComDetailGo.enabled = False

    Call Sub()
    imgbtnComDetailGo.enabled = True

它似乎不会立即禁用该按钮。

更新 - 我在谷歌上找到了这段代码

OnClientClick="this.disabled = true" UseSubmitBehavior="true"

但它似乎没有调用 SUB,因为除了按钮被禁用之外什么都没有发生。

【问题讨论】:

  • 尝试使用 ajax 来做到这一点。
  • 我认为这是 ASP.NET?
  • 好的 - 我已经添加了标签。对于未来的问题,请酌情标记 WinForms/ASP.NET/WPF 等,因为这有助于获得正确答案
  • 没问题,谢谢
  • 您能发布您的 button1 点击事件标记吗?

标签: asp.net vb.net button


【解决方案1】:

从后面的代码中将 javascript 附加到按钮(例如 Page_Load 事件):

sJavaScript = "this.value='Please wait...';" ' optional button text 
sJavaScript &= "this.disabled = true;"
sJavaScript &= Page.ClientScript.GetPostBackEventReference(imgbtnComDetailGo, "")

imgbtnComDetailGo.OnClientClick = sJavaScript

如果您的页面有验证器,请在分配给按钮的 OnClientClick 之前使用附加脚本包装上述 sJavaScript:

sJavaScript = "if (typeof(Page_ClientValidate) == 'function') {if (Page_ClientValidate()) {" & sJavaScript & ";}}"

【讨论】:

    【解决方案2】:

    我已经用一些脚本完成了这个。这是代码:

    在您的实用程序文件中使用它并在需要时调用

    C#

    public static void SubmitDisable(System.Web.UI.Page source, ref System.Web.UI.WebControls.Button button)
            {
                string script = null;
    
                script += "if (typeof(Page_ClientValidate) == 'function')";
                script += "{";
                //script += "   /if client-side does not validate, stop (this supports validation groups)";
                //script += "   //however before validating attempt, must save and, then restore the page validation / submission state, otherwise";
                //script += "   //when the validation fails, it prevents the FIRST autopostback from this/other controls";
                script += " var oldPage_IsValid = Page_IsValid;";
                script += " var oldPage_BlockSubmit = Page_BlockSubmit;";
                script += " if (Page_ClientValidate('" + button.ValidationGroup + "') == false)";
                script += " {";
                script += "     Page_IsValid = oldPage_IsValid;";
                script += "     Page_BlockSubmit = oldPage_BlockSubmit;";
                script += "     return false;";
                script += " }";
                script += "}";
    
                //script += "//change button text and disable it";
                script += "this.className = 'ButtonPleaseWait';";
                script += "this.value = 'Working ...';";
                script += "this.disabled = true;";
    
                //script += "//insert the call to the framework JS code that does the postback of the form in the client";
                //script += "//The default code generated by ASP (WebForm_DoPostbackWithOptions) will not ";
                //script += "//submit because the button is disabled (this is new in 2.0)";
                script += source.ClientScript.GetPostBackEventReference(button, null) + ";";
    
                //script += "//MUST RETURN AFTER THIS, OTHERWISE IF THE BUTTON HAS UseSubmitBehavior=false";
                //script += "//THEN ONE CLICK WILL IN FACT CAUSE 2 SUBMITS, DEFEATING THE WHOLE PURPOSE";
                script += "return true;";
    
                button.Attributes.Add("onclick", script);
            }
    

    在页面加载时,您只需调用 Util.SubmitDisable(this,buttonID);

    CSS:

    .ButtonPleaseWait { background: #F3F3F3 url('../images/pleasewait.gif') no-repeat 2px center; border: thin solid #000; width: 100px; height: 25px; color: #333; font-weight: bold; text-indent: 15px; font-size: 1.13em; }
    

    asp.net

    只需在您的 asp.net 按钮中使用 UseSubmitBehavior="false"

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2014-10-21
      • 1970-01-01
      • 1970-01-01
      • 2020-11-08
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多