简体   繁体   中英

ASP.Net button Disable

I am disabling the button using the javascript code but my server side event is not firing..Any idea?

Javascript
------------------------

        function DisableButton() {
            if (typeof (Page_ClientValidate) == 'function') {
                Page_ClientValidate();
            }
            if (Page_IsValid) {
               document.getElementById('<%=BtnToprimaryValidation.ClientID%>').disabled = true;
                document.getElementById('<%=BtnToprimaryValidation.ClientID%>').value = 'Please wait ...';
            }
        }

ASP.Net
-------------------------
<asp:Button ID="BtnToprimaryValidation" runat="server" 
                        meta:resourcekey="BtnToprimaryValidationResource1" 
                       CssClass="submitButton">

Server Side c#
---------------------------

        protected void Page_Load(object sender, EventArgs e)
        {
            BtnToprimaryValidation.Click += BtnToprimaryValidationClick;
        }
         protected void BtnToprimaryValidationClick(object sender, EventArgs e)
        {
        }

Thanks in Advance, Rahul R

Events do not fire on disabled items

Instead of disabling, you'll have to change your DisableButton method to return false , like so:

function DisableButton() {
        if (typeof (Page_ClientValidate) == 'function') {
            Page_ClientValidate();
        }
        if (Page_IsValid) {
            document.getElementById('<%=BtnToprimaryValidation.ClientID%>').value = 'Please wait ...';
        }
        else {
            return false;
        }
}

As stated in the previous answer by @mattytommo , events do not fire on disabled buttons .

Use a short timeout to fire the event first and then disable the button:

setTimeout(function(){ 
document.getElementById('<%=BtnToprimaryValidation.ClientID%>').disabled = true; 
}, 100);

Just like I suggested on this question .

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM