【问题标题】:Check Positive Negative in Eval & Change color在 Eval 中检查正负并更改颜色
【发布时间】:2015-12-05 19:40:37
【问题描述】:

我想更改 .aspx 中标签的前景色 我试图比较值是否> 0,那么它应该是绿色,如果不是,那么它应该是红色

问题出在条件部分 我尝试使用 int、double、float .. 但非工作 我总是得到 Specified cast is not valid.

这是我的台词

<asp:Label ID="TAmtLabel" runat="server" Text='<%# Eval("TAmt", "{0:c}") %>' Font-Size="13pt" Font-Bold="true" ForeColor='<%# (int)Eval("TAmt") > 0 ? "#00C000" : "#C00000" %>' />

【问题讨论】:

  • 我们可以知道您的 ConvertFromHexToColorEval 中有什么代码吗?
  • 那不应该在那里它来自复制粘贴。我删除了它
  • 你看这个帖子stackoverflow.com/questions/368169/…了吗?
  • 这是不同的情况,对我的情况没有帮助。我知道如何在 eval 中执行 if 语句,但问题是数据类型

标签: c# .net if-statement eval


【解决方案1】:

这是我的方法,希望有所帮助:)

我使用 CssClass 代替 ForeColor..

<asp:Label ID="TAmtLabel" runat="server" Text='<%# Eval("TAmt", "{0:c}") %>' Font-Size="13pt" Font-Bold="true" CssClass='<%# Convert.ToDouble(Eval("TAmt")) > 0 ? "less" : "more" %>' />

并在你的 css 中实现类似的东西

<style type="text/css">
    .less
    {
        color : #00C000;
    }
    .more
    {
        color : #C00000;
    }
</style>

转换 html 颜色和这个话题一样复杂..convert hex code to color name

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2013-01-26
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多