Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to access the item being data bound during ItemDataBound?

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;
   ...
}
like image 246
Ian Boyd Avatar asked Dec 05 '08 14:12

Ian Boyd


People also ask

How do you call an ItemDataBound event in C#?

You should assign RadGrid. DataBind(); method to RadTextBox. TextChanged even. So you will get your ItemDataBound event fires.

Which of the following events of DataList provides you with the last opportunity to access the data item before it is displayed to the client?

The ItemDataBound event is raised after an item is data bound to the DataList control. This event provides you with the last opportunity to access the data item before it is displayed on the client. After this event is raised, the data item is no longer available.

What is ItemDataBound in asp net?

The ItemDataBound event occurs for each new item that is added to the Items collection of RadTimeline when it is bound. This event only occurs if the items are loaded from a data source (the DataSource or DataSourceID property is set).


1 Answers

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
        }
      }
    }
like image 168
Quantum Dynamix Avatar answered Sep 19 '22 14:09

Quantum Dynamix