How to use a ASP.NET Eval() function in a ternary operator?

ⅰ亾dé卋堺 提交于 2019-12-02 03:22:36

问题


I am looking to evaluate two strings from my dataset to identify a class description using a ternary operator. I continue to get a compiler error when running this code stating that "Expression Expected". I think that it has to do with the comparison of strings but I have tried other comparison operators and can't seem to get it to work.

<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>

Here is my sample data:

    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

Thank you for your help


回答1:


I think the problem is that you are trying to do comparison between two strings. Just convert the values to a int or something similar for comparison. So for example, change your comparison to something like the below:

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

Or you can just cast it to the appropriate type:

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

Hope this helps!




回答2:


Try this

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


来源:https://stackoverflow.com/questions/11387618/how-to-use-a-asp-net-eval-function-in-a-ternary-operator

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!