我正在尝试将实现 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/