5
votes

I am using the ListView Control with the following datasource List<MyObject>

On my listview control i have an OnItemDataBound

My question is how do get the current value of MyObject. Ie myObj[5].FirstName

protected void ItemsListViewDataBound(object sender, ListViewItemEventArgs e) { // I want to do some kind of a cast here

}

2
I think you'll have to try to reword the question to get any replies. I'm having a hard time following exactly what you mean. Some sample code would help. - Jon Skeet
ok thanks, appreciate the feed back. - frosty
Unless I missed the ball with my answer I think I got the question... - Aaron Powell

2 Answers

12
votes
protected void MyListView_DataBind(object sender, ListViewItemEventArgs e){
  if(e.Item.ItemType == ListViewItemType.DataItem){
    MyObject p = (MyObject)((ListViewDataItem)e.Item).DataItem;
  }
}

You'll want to do the type check so that you don't try and do a cast when you're working on say, the header item.

2
votes

this one may help :

void listview1_ItemDataBound(object sender, ListViewItemEventArgs e)
{
    ListViewItem listItem = (ListViewItem)e.Item;
    //or 
    ListViewDataItem listDataItem = (ListViewDataItem)e.Item;

    Label mylabelinItem = listItem.FindControl("labelId") as Label;
}