为什么 Page_Load 处理程序中的 DataItem 始终为 null?
我使用 ASP 转发器创建了 ASP.net 网页。我将 ASP Repeater 与 IOrderedEnumerable
数据源绑定。
我需要访问 Page_Load 事件处理程序内部的 Repeater DataItems。但是,当我尝试获取 repeater.Items[x].DataItem
的值时,我总是得到 null。这里应该总是有一个整数值。
尽管如此,该页面在其他方面呈现良好。为什么我无法在 Page_Load 事件处理程序中访问 RepeaterItems 的 DataItem
属性?
I've created an ASP.net web page with an ASP Repeater. I bind the ASP Repeater with an IOrderedEnumerable<int>
DataSource.
I need to access the Repeater DataItems at inside the Page_Load event handler. However, when I try to get the value of repeater.Items[x].DataItem
, I always get null. There should always be an integer value here.
In spite of this, the page otherwise renders fine. Why can't I access the DataItem
property of my RepeaterItems inside the Page_Load event handler?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
您的
Repeater
直到页面生命周期的后期才会进行数据绑定。如果您想在Page.Load
处理程序中引用Repeater.Items[i].DataItem
,请首先尝试提前绑定Repeater
:Your
Repeater
doesn't databind until later in the page lifecycle. If you want to referenceRepeater.Items[i].DataItem
in aPage.Load
handler, try to early-bind theRepeater
first:它仅在数据绑定期间可用。
Its only available during databinding.
正如其他人所说,它仅在数据绑定期间可用。您需要连接中继器的 OnItemDataBound 事件。在其事件处理程序中,您可以执行以下操作:
As everyone else has said, it's only available during databinding. You will need to wire up the OnItemDataBound event of the repeater. In its event handler, you can do this:
数据项仅在数据绑定过程发生后才存在。在中继器中的回发过程中唯一保留的是在视图状态中序列化的控件属性。如果您需要这些项目中的数据,您可以像前面所说的伪编码器和数据绑定一样,或者如果不可能,您可以编写一个实用程序函数,该函数采用中继器数据项并从中继器中的控件中提取它(假设您存储了所有数据)您在这些控件中需要的信息以某种方式)
The data items only exist after the databinding process has taken place. The only thing that is preserved across postbacks in the repeater are the control properties that are serialized in viewstate. If you need the data in those items, you can do like pseudocoder said and databind earlier or if that is not possible you could write a utility function that takes a repeater data item and extracts it from the controls in your repeater (assuming you stored all the information you need in those controls somehow)