单击时禁用按钮,然后在刷新后启用

时间:2013-06-18 09:45:43

标签: asp.net vb.net button

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

当用户点击提交按钮时,我想立即禁用它,然后运行sub,一旦sub已经运行,再次启用该按钮。

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

        imgbtnComDetailGo.enabled = False

    Call Sub()
    imgbtnComDetailGo.enabled = True

它似乎没有立即禁用该按钮。

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

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

但它似乎没有调用SUB,因为除了按钮被禁用之外没有任何反应。

2 个答案:

答案 0 :(得分:0)

从后面的代码中将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 & ";}}"

答案 1 :(得分:0)

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

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

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