• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 333
  • Last Modified:

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;" />
0
dtburdick
Asked:
dtburdick
1 Solution
 
TimCotteeCommented:
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
 
TimCotteeCommented:
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
Concerto Cloud for Software Providers & ISVs

Can Concerto Cloud Services help you focus on evolving your application offerings, while delivering the best cloud experience to your customers? From DevOps to revenue models and customer support, the answer is yes!

Learn how Concerto can help you.

 
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
 
Sammy AgeilCommented:
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
 
RomModCommented:
Question closed - 125 points refunded.

Best regards,
RomMod
Experts Exchange
Community Support Moderator
0

Featured Post

[Webinar] Cloud and Mobile-First Strategy

Maybe you’ve fully adopted the cloud since the beginning. Or maybe you started with on-prem resources but are pursuing a “cloud and mobile first” strategy. Getting to that end state has its challenges. Discover how to build out a 100% cloud and mobile IT strategy in this webinar.

Tackle projects and never again get stuck behind a technical roadblock.
Join Now