根据情况更改列表视图排颜色 [英] Changing listview row color according to conditions

查看:147
本文介绍了根据情况更改列表视图排颜色的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在开发上写报告网站......用户可以在报表发表意见(呵呵!就好象一个新啄;))..回question..what我做的是显示了注释使用列表视图

i am developing a website on writing reports ...users can comment on a report (huh!! as if a new thingy ;))..back to question..what am doing is showing the comments using a listview.

问:我想,当有人对自己的信息发表评论行应该是不同的颜色,以便其清晰可见的消息发布者曾评论...我在看到了这一点斯科特Guthrie的 blog..any帮助AP preciated.Thanks。

Question: i want that when someone comments on his own post the row should be of different color, so that its clearly visible that the post owner has commented...I have seen this in Scott Guthrie's blog..any help is appreciated.Thanks.

推荐答案

您可以用数据键和的ItemDataBound 事件做到这一点。

You can do this with data keys and the ItemDataBound event.

<asp:ListView ID="ListView1" runat="server" DataKeyNames="UserID" OnItemDataBound="ListView1_ItemDataBound" ... />

在除了上述,您还需要在服务器控件包每个项目,所以你可以调整背景色:

In addition to the above, you'll also need to wrap each item in a server control so you can adjust the background color:

<ItemTemplate>
    <asp:Panel ID="Panel1" runat="server">
        <!-- the item content here -->
    </asp:Panel>
</ItemTemplate>

下面是一些例子code-背后:

Here's some example code-behind:

protected void ListView1_ItemDataBound(object sender, ListViewItemEventArgs e)
{
    var dataItem = e.Item as ListViewDataItem;
    if (dataItem != null)
    {                        
        var innerPanel = dataItem.FindControl("Panel1") as Panel;
        if (innerPanel!= null)
        {
            var userID = (int)ListView1.DataKeys[dataItem.DisplayIndex]["UserID"];
            if (userID == base.User.UserID)
                innerPanel.BackColor = Color.PeachPuff;
        }
    }        
}

这篇关于根据情况更改列表视图排颜色的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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