[英]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. 我在代码隐藏的ASP.NET中有一个布尔属性,现在我想在标记文件的Javascript中使用它,但Javascript不理解True或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 谢谢
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" %>)
Which will appear to the client as: 这对客户来说是:
if (true)
or 要么
if (false)
Option A may look a bit funny but it works fine. 选项A可能看起来有点滑稽,但它工作正常。 Option C produces pretty client-side code that probably nobody will see.
选项C产生漂亮的客户端代码,可能没有人会看到。 My preference is Option B.
我的偏好是选项B.
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.