views:

1375

answers:

2

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

}

+6  A: 
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.

Slace
+1  A: 

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;
}
Moran