数据列表itemdatabound事件有问题,条件是更改项目bg颜色 [英] datalist itemdatabound event having issues changing item bg color on condition

查看:61
本文介绍了数据列表itemdatabound事件有问题,条件是更改项目bg颜色的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

伙计们,我正在尝试做一些非常简单的事情.我正在检查数据行中的数据列是否大于0,我希望数据列表中的项目背景色为绿色,如果<0保持透明...

Hey guys I'm trying to do something really simple.. I'm checking a data column in my datarow if it's > 0 I want the item back color in the datalist to be green if its < 0 remain transparent...

if (e.Item.ItemType == ListItemType.Item ||
         e.Item.ItemType == ListItemType.AlternatingItem)
    {
        DataRowView drv = (DataRowView)(e.Item.DataItem);
        int rating = int.Parse(drv.Row["rating"].ToString());

        if (rating > 0)
        {
            e.Item.BackColor = System.Drawing.Color.Green;
        }

    }

我已经调试过调试器,并且它达到了颜色不变的所有条件..我知道它一定很简单,只是看不见.

I've stepped through with debugger and it's hitting all the conditions the color just isn't changing.. I know it has to be something simple I just can't see it.

推荐答案

您需要使用e.Item.FindControl实例化要更改其背景颜色的控件的实例.

You need to use the e.Item.FindControl to instantiate an instance of the control you want to change the background color of.

if (e.Item.ItemType == ListItemType.Item ||
     e.Item.ItemType == ListItemType.AlternatingItem)
    {
        DataRowView drv = (DataRowView)(e.Item.DataItem);
        int rating = int.Parse(drv.Row["rating"].ToString());

        if (rating > 0)
        {
            Label lbl = (Label)e.Item.FindControl("yourLabelIDHere");
            lbl.BackColor = System.Drawing.Color.Green;

        }
    }

这篇关于数据列表itemdatabound事件有问题,条件是更改项目bg颜色的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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