简体   繁体   English

数据列表itemdatabound事件有条件更改项目bg颜色

[英]datalist itemdatabound event having issues changing item bg color on condition

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... 大家好,我正在尝试做一件非常简单的事情。我正在检查数据行中的数据列是否大于0,如果它的<0保持透明,我希望数据列表中的项目背景色为绿色...

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. 我已经调试过调试器,它已经达到了颜色不变的所有条件。.我知道它一定很简单,只是看不见。

You need to use the e.Item.FindControl to instantiate an instance of the control you want to change the background color of. 您需要使用e.Item.FindControl实例化要更改其背景颜色的控件的实例。

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;

        }
    }

Where are the putting this code? 将此代码放在哪里? It needs to be on the OnRowDataBound() event. 它必须在OnRowDataBound()事件上。 It looks like you might be putting the above in OnItemDataBound() . 看起来您可能将上面的内容放在OnItemDataBound()

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM