开发者

How do I change a ListView dynamically on DataBound?

I have a ListView with a template, it puts a bunch of data in, like X Y Z.

I want to hide show some columns based on criteria, so I have ItemDataBound event, but I don't know how to get the actual listview row so I can do things to do i开发者_如何学JAVAt.

Any ideas?


You can access the ListViewItemEventArgs' Item property to get at the current item (the one being bound to data).

The sample code below (which shows how to customize a ListView item in the ItemDataBound event) was taken from the MSDN documentation:

protected void ContactsListView_ItemDataBound(object sender, ListViewItemEventArgs e)
{
    Label EmailAddressLabel;
    if (e.Item.ItemType == ListViewItemType.DataItem)
    {
        // Display the e-mail address in italics.
        EmailAddressLabel = (Label)e.Item.FindControl("EmailAddressLabel");
        EmailAddressLabel.Font.Italic = true;

        System.Data.DataRowView rowView = e.Item.DataItem as System.Data.DataRowView;
        string currentEmailAddress = rowView["EmailAddress"].ToString();
        if (currentEmailAddress == "orlando0@adventure-works.com")
        {
            EmailAddressLabel.Font.Bold = true;
        }
    }
}
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜