单击当按钮禁用时,单击按钮不会触发

时间:2016-05-01 07:58:34

标签: javascript jquery asp.net

当我点击asp.net的按钮时,然后在客户端点击我禁用该点击的按钮,然后服务器端点击事件没有触发。

我的代码如下:

<asp:Button ID="ButtonSend2" runat="server" CssClass="CommonButtonStyle" Text="Send Message" OnClientClick="this.disabled='true';return OnClickSendEmail();"
OnClick="btnSend_Click" ValidationGroup="ValidationGroupCompose" />

这是我的Java脚本代码:

function OnClickSendEmail() {
    var value = document.getElementById('CE_ctl00_ContentMain_TextArea_ID').getHTML().replace(/&nbsp;/g, "").trim();
    if (value == "" || value == undefined) {
        $j('#ctl00_ContentMain_lblMessage').text('Message body can\'t be blank!');
        $j('#ctl00_ContentMain_lblMessage').show()    
        return false;
    } else {
        $j('#ctl00_ContentMain_lblMessage').text('');
        console.log("Value is returing true");
        return true;
    }
}

2 个答案:

答案 0 :(得分:1)

禁用该按钮后,不会进行回发。您可以在处理结束时重新启用该按钮,但还有另一个问题:当浏览器忙于处理OnClickSendEmail()时,显示将不会更新,因此该按钮永远不会被禁用。

这是一个可能的解决方案,它涉及首先取消回发并异步处理命令:

<asp:Button ID="ButtonSend2" runat="server" OnClientClick="this.disabled = true; setTimeout(OnClickSendEmail, 0); return false;" ... />

然后在漫长的处理结束时用__doPostBack触发回发:

function OnClickSendEmail() {
    var value = document.getElementById('CE_ctl00_ContentMain_TextArea_ID').getHTML().replace(/&nbsp;/g, "").trim();
    if (value == "" || value == undefined) {
        $j('#ctl00_ContentMain_lblMessage').text('Message body can\'t be blank!');
        $j('#ctl00_ContentMain_lblMessage').show()    
    } else {
        $j('#ctl00_ContentMain_lblMessage').text('');
        console.log("Value is returing true");
        __doPostBack('<%= ButtonSend2.UniqueID %>', '');
    }
}

答案 1 :(得分:0)

在你的javascript代码中,有两点可能导致最后没有触发。我在代码上写下了可能的要点。此外,您已将其包含在ValidationGroupCompose验证中,您确定不会从那里停止吗?

function OnClickSendEmail() {
    // !!! if the element not found is throw an error here and not continue at all.
    var value = document.getElementById('CE_ctl00_ContentMain_TextArea_ID').getHTML().replace(/&nbsp;/g, "").trim();
    if (value == "" || value == undefined) {
        $j('#ctl00_ContentMain_lblMessage').text('Message body can\'t be blank!');
        $j('#ctl00_ContentMain_lblMessage').show()    
        // !!!! if comes here and return false, then is NOT firing, not continue.
        return false;
    } else {
        $j('#ctl00_ContentMain_lblMessage').text('');
        // !!!! if comes here and you not use a browser that support the console, function, is thrown an error and not continue to fire up.
        console.log("Value is returing true");
        return true;
    }
}

调试您的javascript以查看哪些内容出错,同时从最终代码中移除console.log