JavaScript to disable button causes error on page

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;" />
Who is Participating?
Question closed - 125 points refunded.

Best regards,
Experts Exchange
Community Support Moderator
TimCotteeHead of Software ServicesCommented:
Hi dtburdick,

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

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

Tim Cottee
dtburdickAuthor Commented:
It doesn't work with it either.  I was getting desperate and took it out.
Free Tool: ZipGrep

ZipGrep is a utility that can list and search zip (.war, .ear, .jar, etc) archives for text patterns, without the need to extract the archive's contents.

One of a set of tools we're offering as a way to say thank you for being a part of the community.

TimCotteeHead of Software ServicesCommented:

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.

dtburdickAuthor Commented:
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 ).

dtburdickAuthor Commented:
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.
dtburdickAuthor Commented:


That disables the button alright.  I can't do a double click on it, but the single click doesn't work either.
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
dtburdickAuthor Commented:
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.
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.