如何访问项是期间的ItemDataBound绑定的数据? [英] How to access the item being data bound during ItemDataBound?

查看:168
本文介绍了如何访问项是期间的ItemDataBound绑定的数据?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想在那个被绑定的数据的项目,一个asp的ItemDataBound活动期间:转发

我尝试以下(这是在<一个一个未被接受的答案href=\"http://stackoverflow.com/questions/284420/datagridview-bindinghow-to-color-line-depending-of-the-object-binded#284470\">stackoverflow问题):

 保护无效myRepeater_ItemDataBound(对象发件人,RepeaterItemEventArgs E)
{
    对象的DataItem = e.Item.DataItem;
    ...
}

e.Item.DataItem 为null。

我怎样才能访问该项目被称为的ItemDataBound在活动期间绑定数据。我认为,当一个项目被绑定的数据发生的ItemDataBound事件。

我想要得到的对象,以便我可以采取措施来控制它的显示方式,除了对象可以有额外的有益特性让我丰富它的显示方式。

<一个href=\"http://stackoverflow.com/questions/344047/aspnet-how-to-access-the-item-being-data-bound-during-itemdatabound#344073\">Tool有了正确的答案。答案是, e.Item.Data 是时才有效 e.Item.ItemType 是(项目,AlternatingItem) 。其他时候,它是无效的。

:就我而言,我的头(或页脚)行,那里没有DataItem的过程中收到的ItemDataBound事件

 保护无效myRepeater_ItemDataBound(对象发件人,RepeaterItemEventArgs E)
{
   //如果数据绑定项目是一个项目或交替项(不是标题等)
   如果(e.Item.ItemType = ListItemType.Item和放大器;!&安培;
         e.Item.ItemType!= ListItemType.AlternatingItem)
   {
      返回;
   }   对象的DataItem = e.Item.DataItem;
   ...
}


解决方案

右键关闭蝙蝠我会猜你需要这样的:

 如果(e.Item.ItemType == || ListItemType.Item
    e.Item.ItemType == ListItemType.AlternatingItem)
{
    //在这里把东西
}

毕竟,项目本身可以重新presenting页眉或页脚行。

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;
   ...
}

解决方案

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.

这篇关于如何访问项是期间的ItemDataBound绑定的数据?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆