datalist itemdatabound event having issues changing item bg color on condition

别说谁变了你拦得住时间么 提交于 2019-12-25 03:15:23

问题


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.


回答1:


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;

        }
    }



回答2:


Where are the putting this code? It needs to be on the OnRowDataBound() event. It looks like you might be putting the above in OnItemDataBound().



来源:https://stackoverflow.com/questions/2852629/datalist-itemdatabound-event-having-issues-changing-item-bg-color-on-condition

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!