【问题标题】:How to use a ASP.NET Eval() function in a ternary operator?如何在三元运算符中使用 ASP.NET Eval() 函数?
【发布时间】:2017-04-07 14:05:46
【问题描述】:

我希望评估我的数据集中的两个字符串,以使用三元运算符识别类描述。 运行此代码时,我继续收到编译器错误,指出“预期表达式”。 我认为这与字符串的比较有关,但我尝试了其他比较运算符,但似乎无法正常工作。

<ItemTemplate>
<tr>
   <td><%# FormatDateTime(Eval("GameDate"), DateFormat.ShortDate)%></td>
   <td class="<%# (Eval("Team1Score").ToString() > Eval("Team2Score").ToString()) ? 'Winner':'' %>"><%# Eval("Team1")%></td>
   <td><%# Eval("Team1Score")%></td>
   <td><%# Eval("Team2")%></td>
   <td><%# Eval("Team2Score")%></td>
</tr>
</ItemTemplate>

这是我的示例数据:

    GameDate      Team1 Team1Score     Team2    Team2Score      Winner
    2012-04-14    Blues 5              Reds     3               Blues
    2012-04-13    A's   4              B's      2               A's
    2012-04-11    Blues 1              A's      1               Tie
    2012-04-13    B's   3              Reds     2               B's
    2012-04-10    Blues 7              B's      4               Blues

感谢您的帮助

【问题讨论】:

    标签: asp.net eval ternary-operator


    【解决方案1】:

    我认为问题在于您试图在两个字符串之间进行比较。只需将值转换为 int 或类似的东西进行比较。因此,例如,将您的比较更改为如下所示:

    <td class="<%# (Convert.ToInt32(Eval("Team1Score")) > Convert.ToInt32(Eval("Team2Score"))) ? 'Winner':'' %>"><%# Eval("Team1")%></td>
    

    或者您可以将其转换为适当的类型:

    <td class="<%# ((int)Eval("Team1Score") > (int)Eval("Team2Score")) ? 'Winner':'' %>"><%# Eval("Team1")%></td>
    

    希望这会有所帮助!

    【讨论】:

      【解决方案2】:

      试试这个

      <%#Eval("Status").ToString()=="1" ? "ClubMember" : "Free User" %>
      

      【讨论】:

        猜你喜欢
        • 2021-09-30
        • 2020-04-30
        • 2019-07-30
        • 2013-10-16
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2022-01-04
        • 2020-05-10
        相关资源
        最近更新 更多