views:

56

answers:

4

In aspx page:

if (<%= Not Me.ThisVisa.PassportExpirationDate.IsNull %>){   

Returns error:

Microsoft JScript runtime error: 'True' is undefined

I tried this:

if ("<%= Me.ThisVisa.PassportExpirationDate.IsNull.ToString %>" != "True"){   

..but I get a compile time error:

Error   5   Option Strict On disallows implicit conversions from 'String' to 'Long'

Help!

+2  A: 

What about:

if (<%= (Not Me.ThisVisa.PassportExpirationDate.IsNull).ToString().ToLower() %>){  

This will evaluate the Boolean condition and convert it to a string, but you should lower-case it so it looks like JavaScript syntax when it's rendered.

Cory Larson
This is right, but I'd also use an explicit conditional operator to list the string literals, because if run in different localizations this code still might not output what is expected.
Joel Coehoorn
@Joel: Would you recommend doing something like adding quotations around what's there (dropping the `ToLower()`) and comparing it against `Boolean.TrueString`? Would that satisfy the localization problem?
Cory Larson
A: 

Get rid of the extra spaces. Change to:

if ("<%=Me.ThisVisa.PassportExpirationDate.IsNull.ToString%>" != "True"){

Velika
A: 

You are missing the parenthesis for the ToString() method.

update: sry, my bad

Necros
He's doing VB.NET. No parentheses necessary.
kbrimington
+2  A: 

Consider moving the logic into the server script. Doing so will reduce how much JavaScript you emit onto the page.

<% If Not Me.ThisVisa.PassportExpirationDate.IsNull Then %>

// JavaScript Goodness

<% End If %>
kbrimington
even better if this just calls a method that is in a linked .js file
Joel Coehoorn
If the "IF" condition is evaluated to FALSE will this result in the Javascript block NOT being emitted or will the Javascript be emitted unconditionally? I presume the intent is for the former to occur.
Velika
@Velika: Both the intent and the effect. In ASP.NET, on a markup page, content between the <% %>'s executes on the server. It sorta classic-ASP style, and regained some popularity with the MVC framework, but it works just as well in ASP.NET WebForms.
kbrimington