13

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;
   ...
}
Community
  • 1
  • 1
Ian Boyd
  • 220,884
  • 228
  • 805
  • 1,125
  • I personally perfer to do it the way that the answer was done, as it is easier to understand than a randome return to exit especially on a void method like ItemDataBound. My just opinion – Mitchel Sellers Dec 05 '08 at 15:22
  • 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 Dec 05 '08 at 15:03

6 Answers6

17

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.

Răzvan Flavius Panda
  • 20,376
  • 13
  • 101
  • 153
Programmin Tool
  • 6,237
  • 11
  • 46
  • 67
  • Yes, this is needed on a repeater – Mitchel Sellers Dec 05 '08 at 15:24
  • 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 Dec 05 '08 at 15:37
11

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
  • 111
  • 1
  • 2
  • 3
    This was more helpful for me because it has an example of how to retrieve a specific column's information based on the name. – Jacob Morris Jul 24 '15 at 17:41
3

For repeater

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

Can be modified to:

if (e.Item.DataItem != null) ...
Ian Boyd
  • 220,884
  • 228
  • 805
  • 1,125
1

Use dynamic

 dynamic item = e.Item.DataItem;
 string style = item.Style;
Akshay Mishra
  • 927
  • 1
  • 11
  • 13
0

For a repeater with a custom template binding; you can use the following template. I used this to create a table which breaks down each data item into two rows for print view.

Repeater1.HeaderTemplate = new PrintTemplate(ListItemType.Header);
Repeater1.ItemTemplate = new PrintTemplate(ListItemType.Item);
Repeater1.AlternatingItemTemplate = new  PrintTemplate(ListItemType.AlternatingItem);
Repeater1.FooterTemplate = new PrintTemplate(ListItemType.Footer);

 public class PrintTemplate : ITemplate
{
    ListItemType templateType;

  public PrintTemplate(ListItemType type)
  {
     templateType = type;

  }
  public void InstantiateIn(System.Web.UI.Control container)
  {       
     Literal lc = new Literal();

     switch(templateType)
     {
        case ListItemType.Header:
           lc.Text = "<TABLE>";
           container.Controls.Add(lc);
           break;
        case ListItemType.Item:
        case ListItemType.AlternatingItem:
           //lc.Text = "<TR><TD>";
           lc.DataBinding += new EventHandler(TemplateControl_DataBinding);
           container.Controls.Add(lc);
           break;
        case ListItemType.Footer:
           lc.Text = "</TABLE>";
           container.Controls.Add(lc);
           break;
     }
  }

    private void TemplateControl_DataBinding(object sender,
    System.EventArgs e)
          {

              Literal lc;
              lc = (Literal)sender;
              RepeaterItem container = (RepeaterItem)lc.NamingContainer;
              ListItemType itmType = container.ItemType;

              //construct the repeater row using a custom function that switches on item type; HEADER vs ITEM vs ALTERNATINGITEM
              lc.Text += GetPopulatedRepeaterRow(dataInterface, container.DataItem, container.ItemType); 
              ...
civilator
  • 89
  • 3
0

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
  • 355
  • 1
  • 3
  • 12
  • Although the title doesn't, the first line of the question does; but StackOverflow thanks you for additional helpful material. – Ian Boyd Mar 12 '09 at 15:02