ASP.NET: How to access the item being data bound during ItemDataBound?
<p>i want to get at the item that is being data bound, during the ItemDataBound event of an asp:repeater.</p>

<p>i tried the following (which was an unaccepted answer in a <a href="http://stackoverflow.com/questions/284420/datagridview-bindinghow-to-color-line-depending-of-the-object-binded#284470">stackoverflow question</a>):</p>

<pre><code>protected void myRepeater_ItemDataBound(object sender, RepeaterItemEventArgs e)
{
    Object dataItem = e.Item.DataItem;
    ...
}
</code></pre>

<p>but <code>e.Item.DataItem</code> is null.</p>

<p>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.</p>

<p>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.</p>

<h2>Answer</h2>

<p><a href="http://stackoverflow.com/questions/344047/aspnet-how-to-access-the-item-being-data-bound-during-itemdatabound#344073">Tool</a> 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:</p>

<pre><code>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 &amp;&amp; 
         e.Item.ItemType != ListItemType.AlternatingItem)
   {
      return;
   }

   Object dataItem = e.Item.DataItem;
   ...
}
</code></pre>
