Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Javascript: How to check boolean value from code-behind in ASP.NET

I have a boolean property in code-behind ASP.NET, now I want to use it in Javascript of mark-up file, but Javascript do not understand True, or False. SO now, I'm using this:

if ( '<%=IsTabVisible%>' == 'True'){
///
}

It works, but quite ugly. Is there a better way to do this?

Thank you

like image 652
Quan Mai Avatar asked Jul 28 '11 06:07

Quan Mai


1 Answers

The way I see it you have three options:

A. Do what you are already doing.

B. Perform the if test on the server-side, something like this:

<% if (IsTabVisible) { %>
    // client-side code here, whatever you had inside the brackets
    // of your original if statement
<% } %>

C. Make sure you generate 'true' and 'false' in lowercase so that it will work as client-side JavaScript, something like this:

if (<%= IsTabVisible ? "true" : "false" %>)

Which will appear to the client as:

if (true)

or

if (false)

Option A may look a bit funny but it works fine. Option C produces pretty client-side code that probably nobody will see. My preference is Option B.

like image 173
nnnnnn Avatar answered Oct 25 '22 21:10

nnnnnn