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

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

概述在传统的C#代码块中: "myInt = (<condition> ? <true value> : <false value>)" 但是如何在.aspx中使用我想要response.write有条件地使用: <% ( Discount > 0 ? Response.Write( "$" + Html.Encode(discountDto.Discount.FlatOff.ToString("# 在传统的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>

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

总结

以上是内存溢出为你收集整理的将“in-line IF”(C#)与response.write相结合全部内容,希望文章能够帮你解决将“in-line IF”(C#)与response.write相结合所遇到的程序开发问题。

如果觉得内存溢出网站内容还不错,欢迎将内存溢出网站推荐给程序员好友。

欢迎分享,转载请注明来源:内存溢出

原文地址: http://outofmemory.cn/langs/1236155.html

(0)
打赏 微信扫一扫 微信扫一扫 支付宝扫一扫 支付宝扫一扫
上一篇 2022-06-06
下一篇 2022-06-06

发表评论

登录后才能评论

评论列表(0条)

保存