9

従来のC#コードブロックでは:

"myInt = (<condition> ? <true value> : <false value>)"

しかし、条件付きでresponse.writeしたい.aspx内での使用はどうでしょうか。

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

mny thx

4

3 に答える 3

19

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>

<%#%>マークアップ拡張機能の興味深い点は、タグの属性内で使用できるのに対し、他の2つの形式(<%と<%=)はタグコンテンツでのみ使用できることです(いくつかの特別なものがあります)ケースの例外)。上記の例はこれを示しています。

于 2009-11-30T16:09:34.113 に答える
9
<%=
    (Discount > 0)
        ? "$" + Html.Encode(discountDto.Discount.FlatOff.ToString("#,###."))
        : ""
%>
于 2009-11-30T15:59:17.473 に答える
3

Response.Writeを全体に配置しますか?:-操作:

<% Response.Write( Discount > 0 ? "$" + Html.Encode(discountDto.Discount.FlatOff.ToString("#,###.") : "" ) %>
于 2009-11-30T16:01:21.197 に答える