0

I have a javascript call in OnClientClick that looks like the following:

OnClientClick='<%# "return DisplayAlert(" + Eval("BedNum") + "," + Eval("ClientId") + ");" %>'

But when ClientId is null, the function call is constructed incorrectly with error Uncaught SyntaxError: Unexpected token ) since the function call produces return DisplayAlert(value, );

Is there a way to avoid this error when the 2nd parameter is null?

Thanks

fdkgfosfskjdlsjdlkfsf
  • 3,165
  • 2
  • 43
  • 110
  • How in the world is that a duplicate? You're suggesting adding `ToString().Length`? – fdkgfosfskjdlsjdlkfsf Apr 23 '16 at 19:46
  • 1
    It is not a duplicate that asks/answers **exactly the same** question. But it is a duplicate that needs to solve basically **the same problem** - to Eval or not to Eval something conditionally (in your case comma+second argument). So it at least should give you some ideas. – Eugene Podskal Apr 23 '16 at 19:48
  • You're right and I apologize. I did, and I ended up using something like this: `OnClientClick='<%# "return ShowRoom(" + Eval("BedNum") + "," + Eval("ClientId").ToString().Length + ");" %>'` – fdkgfosfskjdlsjdlkfsf Apr 23 '16 at 19:51
  • That way, if 2nd parameter is `null` then I get 0. Otherwise, I get a positive value. Thanks again. – fdkgfosfskjdlsjdlkfsf Apr 23 '16 at 19:52
  • Does this http://stackoverflow.com/questions/894860/set-a-default-parameter-value-for-a-javascript-function fix your problem right way ? – Syed Ekram Uddin Apr 23 '16 at 19:52

1 Answers1

0

If I understand correctly, all you would need to do is check its value, and for null have a default value of your choosing, or the original clientId in the case that it isn't null.

Something like this:

ClientId == null ? "whateverDefaultValueYouWant" : clientId
smaili
  • 1,245
  • 9
  • 18