简体   繁体   English

ASPX页面中的三元运算符

[英]Ternary operator in aspx page

Hi all I would like to use ternary operator in aspx page. 大家好,我想在aspx页面中使用三元运算符。 I am having two public variables in my aspx.cs file as follows 我在aspx.cs文件中有两个公共变量,如下所示

public string currency = "INR";
public decimal amount = 100;

I would like to frame the html tags based on my currency, currently I am doing like this 我想根据币种来设置html标签,目前我正在这样做

<% if (currency != "INR")
  {%>
     <span>$<%=amount%></span>
  <%}
  else
  { %>
    <span<%=amount%></span>
  <%} %>

I would like to do it one line 我想一行

<span><% if (currency != "INR") %> $ amout <% : %> </span>

But I am getting error as Invalid expression term ':' so can some one help me if this is possible 但是由于Invalid expression term ':' ,我遇到了错误,因此如果可能的话,有人可以帮助我

A tenary operator works without an if. 细心的运算符无需if就可以工作。 It looks as follows: 它看起来如下:

booleanExpression ? trueValue : falseValue

But you can't treat ASP.NET like PHP, so you will have to put this in one <% %> wrapper 但是您不能像ASP那样对待ASP.NET,因此必须将其放入一个<% %>包装器中。

<span><%= (currency != "INR" ? "$" : "") + amount %></span>

Remove if from expression 从表达式中删除

You need to provide one more value after : 您需要在之后提供更多价值:

<span><%= (currency != "INR" ? "" : "Rs.") + amount %></span>

bytecode77's code looks really awkward. bytecode77的代码看起来确实很尴尬。 I'd suggest: 我建议:

<span><%= (currency != "INR") ? amount : " " %></span>

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM