JavaScript to disable button causes error on page

Posted on 2006-06-07
Last Modified: 2012-06-27
Basically I need to disable a button so it cannot be double-clicked.

I've tried several iterations and they all cause the same error.

This is the code in the Page_Load event:

btnCompleteRegistration.Attributes.Add("OnClick","javascript:" +
btnCompleteRegistration.ClientID + ".disabled=true" +

Here is the error:

Line: 174
Char: 80
Error: Expected';'
Code: 0

This is what is on line 174:

<input type="submit" name="btnCompleteRegistration" value="Complete Registration" onclick="javascript:btnCompleteRegistration.disabled=true__doPostBack('btnCompleteRegistration','')if (typeof(Page_ClientValidate) == 'function') Page_ClientValidate(); " language="javascript" id="btnCompleteRegistration" class="button" style="height:47px;width:152px;" />
Question by:dtburdick
    LVL 44

    Expert Comment

    Hi dtburdick,

    btnCompleteRegistration.Attributes.Add("OnClick","javascript:" +
    btnCompleteRegistration.ClientID + ".disabled=true;" +

    You were missing a ; between the .disabled=true and the __doPostBack...

    Tim Cottee

    Author Comment

    It doesn't work with it either.  I was getting desperate and took it out.
    LVL 44

    Expert Comment


    Actually it seems there is another one missing as well, why that should be I don't know. The GetPostBackEventReference seems to cough up some dodgy script. Notice that there should be another ; between ',") and if(typeof(Page_ if you can get that in there then it will work.
    btnCompleteRegistration.Attributes.Add("OnClick","javascript:" +
    btnCompleteRegistration.ClientID + ".disabled=true;" +
    this.GetPostBackEventReference(btnCompleteRegistration).replace("',"") if"),"',""); if"));

    Might solve it.


    Author Comment

    Hmmm.  That's odd.

    Well, now I'm in escape sequence hell.  I gave up on figuring out the escaping of all of those quotes and opted on this:

    Replace("if (typeof","if (typeof");

    Replace("if \(typeof","if \(typeof");

    But I still get this error:

    "unrecognized escape sequence"  when I hover over the backslash.

    If I don't escape it out it says I'm missing the ).


    Author Comment

    Sorry, I'm a dork.  I didn't think I needed to escape the ( out but the error through me for a loop.  I got the replace working but it still isn't putting in the ";".  I'll keep messing with it and post my results, although I'm a bit concerned I have to do this anyway.

    Author Comment



    That disables the button alright.  I can't do a double click on it, but the single click doesn't work either.
    LVL 27

    Expert Comment

    I use this script to disable the button.

    System.Text.StringBuilder sbValid = new System.Text.StringBuilder();

    sbValid.Append("if (typeof(Page_ClientValidate) == 'function') { ");

    sbValid.Append("if (Page_ClientValidate() == false) { return false; }} ");

    sbValid.Append("this.value = 'Please wait...';");

    sbValid.Append("this.disabled = true;");

    sbValid.Append("document.all.btnSubmit.disabled = true;");

    //GetPostBackEventReference obtains a reference to a client-side script function that causes the server to post back to the page.



    this.btnSubmit.Attributes.Add("onclick", sbValid.ToString());

    It can be found here

    Good luck

    Author Comment

    I ended up doing it this way.

    btnCompleteRegistration.Attributes.Add("OnClick","btnCompleteRegistration.disabled=true;" + this.GetPostBackEventReference(btnCompleteRegistration).ToString()+";");

    I got the solution from:

    I'm not sure why it works and the others didn't, as they should have.

    Thanks folks.

    Accepted Solution

    Question closed - 125 points refunded.

    Best regards,
    Experts Exchange
    Community Support Moderator

    Write Comment

    Please enter a first name

    Please enter a last name

    We will never share this with anyone.

    Featured Post

    How your wiki can always stay up-to-date

    Quip doubles as a “living” wiki and a project management tool that evolves with your organization. As you finish projects in Quip, the work remains, easily accessible to all team members, new and old.
    - Increase transparency
    - Onboard new hires faster
    - Access from mobile/offline

    AJAX ModalPopupExtender has a required property "TargetControlID" which may seem to be very confusing to new users. It means the server control that will be extended by the ModalPopup, for instance, if when you click a button, a ModalPopup displays,…
    Today is the age of broadband.  More and more people are going this route determined to experience the web and it’s multitude of services as quickly and painlessly as possible. Coupled with the move to broadband, people are experiencing the web via …
    This video is in connection to the article "The case of a missing mobile phone (". It will help one to understand clearly the steps to track a lost android phone.
    In this tutorial you'll learn about bandwidth monitoring with flows and packet sniffing with our network monitoring solution PRTG Network Monitor ( If you're interested in additional methods for monitoring bandwidt…

    759 members asked questions and received personalized solutions in the past 7 days.

    Join the community of 500,000 technology professionals and ask your questions.

    Join & Ask a Question

    Need Help in Real-Time?

    Connect with top rated Experts

    13 Experts available now in Live!

    Get 1:1 Help Now