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

2019-02-27 03:54发布

问题:

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