Javascript:如何从ASP.NET中的代码隐藏检查布尔值

时间:2022-08-25 22:04:57

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:

我在代码隐藏的ASP.NET中有一个布尔属性,现在我想在标记文件的Javascript中使用它,但是Javascript不理解True或False。那么现在,我正在使用这个:

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

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

它有效,但非常难看。有一个更好的方法吗?

Thank you

谢谢

1 个解决方案

#1


14  

The way I see it you have three options:

我看到它的方式有三种选择:

A. Do what you are already doing.

A.做你正在做的事情。

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

B.在服务器端执行if测试,如下所示:

<% 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:

C.确保以小写形式生成“true”和“false”,以便它可以作为客户端JavaScript工作,如下所示:

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

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.

选项A可能看起来有点滑稽,但它工作正常。选项C产生漂亮的客户端代码,可能没有人会看到。我的偏好是选项B.

#1


14  

The way I see it you have three options:

我看到它的方式有三种选择:

A. Do what you are already doing.

A.做你正在做的事情。

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

B.在服务器端执行if测试,如下所示:

<% 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:

C.确保以小写形式生成“true”和“false”,以便它可以作为客户端JavaScript工作,如下所示:

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

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.

选项A可能看起来有点滑稽,但它工作正常。选项C产生漂亮的客户端代码,可能没有人会看到。我的偏好是选项B.