c# - 为什么在绑定(bind) asp :net Repeater to a Collection? 时,ItemDataBound 事件上的 e.Item.DataItem 为空

标签 c# asp.net repeater

我正在尝试将实现 ICollection、IEnumerable 和 IList 接口(interface)的集合绑定(bind)到 asp.net 转发器。该集合名为 CustomCollection。所以我将中继器的数据源设置为集合,如下所示:

rptRepeater.DataSource = customCollection;
rptRepeater.Databind();

然后,在 ItemDataBound 事件中,我尝试访问 DataItem 对象,如下所示:

void rptRepeater_ItemDataBound(object sender, RepeaterItemEventArgs e){

object obj = e.Item.DataItem; // DataItem is null for some reason

}

由于某种原因,e.Item.DataItem 对象为空。有谁知道这是为什么,和/或我可以做些什么来获取集合中要数据绑定(bind)到转发器的对象?

谢谢!

最佳答案

在您的 ItemDataBound 事件处理程序中,您需要检查它是否未尝试绑定(bind)到页眉或页脚模板:

if (e.Item.ItemType == ListItemType.Item || e.Item.ItemType == ListItemType.AlternatingItem) {
    object obj = e.Item.DataItem;
}

关于c# - 为什么在绑定(bind) asp :net Repeater to a Collection? 时,ItemDataBound 事件上的 e.Item.DataItem 为空,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/2621219/

相关文章:

javascript - 调用带有绑定(bind)到转发器的参数的 javascript 函数

c# - 不支持约束的通用方法

c# - 如何在C#项目中使用VS2012的自动HLSL编译?

c# - 我怎样才能让我的变量在我的应用程序中使用?

asp.net - 使用gridview时"must declare scalar variable"

ASP.NET 中继器问题

c# - 当字符串中有反斜杠时,IndexOf 失败——为什么?

c# - 解析格式为 "2013-Jan-31"的日期时间会抛出错误

c# - 是否可以在我的 aspx View 中使用 'using' 语句? (ASP.NET MVC)

asp.net - 使用内联代码测试 Container.DataItem