首页 新闻 会员 周边

aspx页面如何添加逻辑判断

0
[已解决问题] 解决于 2009-12-10 18:24

这是我在前台页面要显示的:

<td><%# DataBinder.Eval(Container.DataItem,"ID") %></td>

现在我要根据ID的值的正负,把ID显示成不同的颜色,

所以我加了如下逻辑判断:

if (int.Parse(DataBinder.Eval(this.Repeater1.Items[0], "ID").ToString()) >0)

报:未将对象引用设置到对象的实例

求名位大虾帮助。

问题补充: 实现了,不过用的是这种方式 protected void Repeater1_ItemDataBound(object sender, RepeaterItemEventArgs e) { if (e.Item.ItemType == ListItemType.Item || e.Item.ItemType == ListItemType.AlternatingItem) { SqlDataReader dr = this.Repeater1.DataSource as SqlDataReader; if (!dr["同比"].ToString().StartsWith("-")) { double newText = Math.Round(Convert.ToDouble(dr["同比"].ToString()), 1); Label lbl = (Label)e.Item.FindControl("lb1"); lbl.ForeColor = System.Drawing.Color.Red; lbl.Text = newText + "%↑"; } else { double newText = Math.Round(Convert.ToDouble(dr["同比"].ToString().Replace("-", "")), 1); Label lbl = (Label)e.Item.FindControl("lb1"); lbl.ForeColor = System.Drawing.Color.SpringGreen; lbl.Text = newText + "%↓"; } }
忽然明白的主页 忽然明白 | 初学一级 | 园豆:80
提问于:2009-12-04 08:35
< >
分享
最佳答案
0

这样子写一定是不可以的,不过你可以选择一个折中的办法,在aspx.cs中定义一格方法比如

public string ChangeColor(string val)

{

    if(val!=""){

  if(int.Parse(val)>0)

    return "<font color='blue'>"+val+"</font>";

  else

    return "<font color='red'>"+val+"</font>";

}

}

然后页面上你可以<td><%# ChangeColor(DataBinder.Eval(Container.DataItem,"ID") )%></td>

由于直接敲的代码 请不要复制粘贴

西越泽 | 专家六级 |园豆:10775 | 2009-12-04 09:01
其他回答(1)
0

<%# Convert.ToInt32(DataBinder.Eval(this.Repeater1.Items[0], "ID")) > 0 ? "Red" : "Blue" %>

 

数据绑定一定要用<%# %>

James.Ying | 园豆:1472 (小虾三级) | 2009-12-04 08:57
清除回答草稿
   您需要登录以后才能回答,未注册用户请先注册