如何访问 ASP.NET ListView 的 DataBound 事件中绑定的 DataSource 项?
我知道问题已经发布 这里,但我们没有找到真正的解决方案。
我已将 ListView 绑定到 SqlDataSource,并且想根据返回的行的某些属性在 LayoutTemplate 中创建的视图中存在的控件中写入一些文本。
显然,我正在使用 ItemDataBound 事件来提供我的项目,但这不是重点。
自发的解决方案是绑定 ListView.DataBound 事件并访问原始数据源(DataTable?)并执行所需的计算。
我检查了 Items 属性,尽管它不为空,但相关的 DataItem 属性为 null。
您有什么建议吗?
我能想到的唯一解决方法是在 ItemDataBound 事件中执行计算并将结果累积在一些私有字段中。但它确实很难看并且使得获得一些所需的值变得更加困难。
多谢。
I know the question has already been posted here but we didn't get to an real solution.
I have bound my ListView to an SqlDataSource and I want to write some text in a control present in the view created in the LayoutTemplate depending on some properties of the rows returned.
Obviously, I'm using the ItemDataBound event to feed my items but this is not the point.
The spontaneous solution was to bind the ListView.DataBound event and access the raw datasource (a DataTable?) and do the required calculations.
I inspected the Items property and, despite it was not empty, the related DataItem property was null.
Do you have any suggestion?
The only work-around I can come to is to execute the calculations in the ItemDataBound event and accumulate the result in some private fields. But it's really ugly to see and makes harder to get some of the required values.
Thanks a lot.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
在 ItemDataBound 中,您应该能够通过 DataSource 属性访问 Listview 的数据源(您可能需要将其转换为 DataTable):
In the ItemDataBound you should be able to access the data source for the Listview through the DataSource property (you might need to cast it to a DataTable):