Sourabh Somani Sourabh Somani - 7 months ago 19
Javascript Question

On Click When Button is Disabling then Click Button is not firing

When I am Clicking on button of asp.net then on client click I am disabling that clicked button then server side click event is not firing.

My code is as following:

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


and This is my Java script code:

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;
}
}

Answer

Once the button is disabled, the postback is not made. You could re-enable the button at the end of the processing but there is another problem: the display will not be updated when the browser is busy processing OnClickSendEmail(), so the button will never look disabled.

Here is a possible solution, which involves canceling the postback at first and processing the command asynchronously:

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

The postback is then triggered with __doPostBack at the end of the lengthy processing:

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 %>', '');
    }
}
Comments