加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 百科 > 正文

将“in-line IF”(C#)与response.write相结合

发布时间:2020-12-15 17:40:23 所属栏目:百科 来源:网络整理
导读:在传统的C#代码块中: "myInt = (condition ? true value : false value)" 但是如何在.aspx中使用我想要response.write有条件地使用: % ( Discount 0 ? Response.Write( "$" + Html.Encode(discountDto.Discount.FlatOff.ToString("#,###."): "")% mny thx
在传统的C#代码块中:
"myInt = (<condition> ? <true value> : <false value>)"

但是如何在.aspx中使用我想要response.write有条件地使用:

<% ( Discount > 0 ?  Response.Write( "$" + Html.Encode(discountDto.Discount.FlatOff.ToString("#,###."): "")%>

mny thx

解决方法

值得了解ASP.NET模板标记处理中不同的标记标签是什么意思:
<% expression %>   - evaluates an expression in the underlying page language
<%= expression %>  - short-hand for Response.Write() - expression is converted to a string and emitted
<%# expression %>  - a databinding expression that allows markup to access the current value of a bound control

所以要发出一个三元表达式(err条件运算符)的值,你可以使用:

<%= (condition) ? if-true : if-false %>

或者你可以写L

<% Response.Write( (condition) ? if-true : if-false ) %>

如果您正在使用数据绑定控件(例如中继器),则可以使用数据绑定格式来评估并发出结果:

<asp:Repeater runat='server' otherattributes='...'>
     <ItemTemplate>
          <div class='<%# Container.DataItem( condition ? if-true : if-false ) %>'>  content </div>
     </ItemTemplate>
 </asp:Repeater>

<%#%>的一个有趣的方面标记扩展是它可以在标签的属性内部使用,而其他两种形式(<%和<%=)只能在标签内容中使用(有一些特殊情况例外).上面的例子说明了这一点.

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读