如何使用ASP.NET eval()函数中的一个三元运算符? [英] How to use a ASP.NET Eval() function in a ternary operator?

查看:195
本文介绍了如何使用ASP.NET eval()函数中的一个三元运算符?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我期待从我的数据评估两个字符串,以确定使用三元运算符一类的描述。
我继续运行此code,指出前pression预计时,得到一个编译错误。
我认为,它与琴弦的做对比,但我已经尝试过其他运营商相比,似乎无法得到它的工作。

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>

下面是我的示例数据:

    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

感谢您的帮助。

推荐答案

我认为问题是,你想两个字符串之间做对比。刚值转换为int或比较类似的东西。因此,例如,你的比较更改为类似下面的:

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>

希望这有助于!

这篇关于如何使用ASP.NET eval()函数中的一个三元运算符?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆