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" +
this.GetPostBackEventReference(btnCompleteRegistration));

Here is the error:

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

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

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

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

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

Tim Cottee
0
 
dtburdickAuthor Commented:
It doesn't work with it either.  I was getting desperate and took it out.
0
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:
dtburdick,

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.


Tim
0
 
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 ).



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

btnCompleteRegistration.Attributes.Add("OnClick","btnCompleteRegistration.disabled=true;");

That disables the button alright.  I can't do a double click on it, but the single click doesn't work either.
0
 
SammyCommented:
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.

sbValid.Append(this.Page.GetPostBackEventReference(this.btnSubmit));

sbValid.Append(";");

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

It can be found here
http://msmvps.com/blogs/anguslogan/archive/2004/12/22/27223.aspx

Good luck
0
 
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:
http://discuss.fogcreek.com/dotnetquestions/default.asp?cmd=show&ixPost=3592

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

Thanks folks.
0
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.