tags:

views:

2484

answers:

5

i want to get at the item that is being data bound, during the ItemDataBound event of an asp:repeater.

i tried the following (which was an unaccepted answer in a stackoverflow question):

protected void myRepeater_ItemDataBound(object sender, RepeaterItemEventArgs e)
{
    Object dataItem = e.Item.DataItem;
    ...
}

but e.Item.DataItem is null.

How can i access the item being data bound during the event called ItemDataBound. i assume the event ItemDataBound happens when an item is being data bound.

i want to get at the object so i can take steps to control how it is displayed, in addition the object may have additional helpful properties to let me enrich how it is displayed.

Answer

Tool had the right answer. The answer is that e.Item.Data is only valid when e.Item.ItemType is (Item, AlternatingItem). Other times it is not valid. In my case, i was receiving ItemDataBound events during header (or footer) rows, where there is no DataItem:

protected void myRepeater_ItemDataBound(object sender, RepeaterItemEventArgs e)
{
   // if the data bound item is an item or alternating item (not the header etc)
   if (e.Item.ItemType != ListItemType.Item && 
         e.Item.ItemType != ListItemType.AlternatingItem)
   {
      return;
   }

   Object dataItem = e.Item.DataItem;
   ...
}
+5  A: 

Right off the bat I would have to guess you need this:

  if(e.Item.ItemType == ListItemType.Item 
       || e.Item.ItemType == ListItemType.AlternatingItem)
    {
        //Put stuff here
    }

After all, the item itself could be representing a header or footer row.

Programmin Tool
Yes, this is needed on a repeater
Mitchel Sellers
Yeah I had actually edited this a couple time because in testing it didn't fail without the check so having little experience with repeaters made me think they didn't. Of course I kind of didn't actually add a Header to test... Brilliant I am.
Programmin Tool
A: 

I believe the event is fired after the item is databound. I think I used to access data in the PreRender event. Would need to check some old code though

Damien
A: 

If you're dealing with an asp:ListView, you can do something like this:

    protected void myLV_ItemDataBound(object sender, ListViewItemEventArgs e)
{
 if (e.Item.ItemType != ListViewItemType.DataItem)
  return;

 object dataItem = ((ListViewDataItem)e.Item).DataItem;

}

(The title of the question doesn't mention an asp:repeater.. so I thought it might be helpful to post the code for an asp:listview)

Adam Douglass
Although the title doesn't, the first line of the question does; but StackOverflow thanks you for additional helpful material.
Ian Boyd
+1  A: 

For repeater

if (e.Item.ItemType != ListItemType.Item && e.Item.ItemType...

Can be modified to:

if (e.Item.DataItem != null) ...
A: 

I just wanted to add that I did accomplished this by doing the following:

protected void rpt_ItemDataBound(object sender, RepeaterItemEventArgs e)
    {
      //determine if the row type is an Item
      if ((e.Item.ItemType == ListItemType.Item) || (e.Item.ItemType == ListItemType.AlternatingItem))
      {
        DataRowView row = (DataRowView)e.Item.DataItem;
        if (row["RowName"].ToString() == "value")
        {
          //INSERT CODE HERE
        }
      }
    }
Quantum Dynamix