How to access the item being data bound during ItemDataBound?
Asked Answered
O

6

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;
   ...
}
Oration answered 5/12, 2008 at 14:54 Comment(2)
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 thoughCatechol
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 opinionAntiparticle
I
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.

Ironbark answered 5/12, 2008 at 15:2 Comment(2)
Yes, this is needed on a repeaterAntiparticle
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.Ironbark
D
12

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
        }
      }
    }
Delaminate answered 8/9, 2010 at 17:1 Comment(1)
This was more helpful for me because it has an example of how to retrieve a specific column's information based on the name.Tetrapody
R
3

For repeater

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

Can be modified to:

if (e.Item.DataItem != null) ...
Redding answered 25/7, 2009 at 9:16 Comment(0)
C
1

Use dynamic

 dynamic item = e.Item.DataItem;
 string style = item.Style;
Cammi answered 22/12, 2017 at 8:10 Comment(0)
N
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)

Nertie answered 11/3, 2009 at 18:35 Comment(1)
Although the title doesn't, the first line of the question does; but StackOverflow thanks you for additional helpful material.Oration
A
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); 
              ...
Arithmetic answered 1/5, 2015 at 21:0 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.