如何访问在ASP.NET ListView的DataBound事件绑定数据源项目? [英] How to access the bound DataSource item in the ASP.NET ListView's DataBound event?

查看:227
本文介绍了如何访问在ASP.NET ListView的DataBound事件绑定数据源项目?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我知道这个问题已经发布<一个href=\"http://stackoverflow.com/questions/1725230/accessing-the-full-datarow-from-the-datasource-in-a-listview-itemdatabound-event\">here但我们没有得到一个真正的解决方案。

I know the question has already been posted here but we didn't get to an real solution.

我已绑定的ListView我到一个SqlDataSource和我想写一个控制present在LayoutTemplate模板依赖于行的某些属性返回创建的视图一些文本。

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.

显然,我使用ItemDataBound事件来养活我的项目,但这不是重点。

Obviously, I'm using the ItemDataBound event to feed my items but this is not the point.

自发解决方案是绑定ListView.DataBound事件,并访问原始数据源(一个DataTable?),然后执行所需的计​​算。

The spontaneous solution was to bind the ListView.DataBound event and access the raw datasource (a DataTable?) and do the required calculations.

我检查项目属性,尽管它不是空的,相关的DataItem属性为null。

I inspected the Items property and, despite it was not empty, the related DataItem property was null.

你有什么建议吗?

唯一的变通办法可以来我是ItemDataBound事件来执行计算和一些私人领域积累的结果。但它确实难看看到并使得更难获得一些必要的值。

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.

非常感谢。

推荐答案

在的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):

protected void Listview1_ItemDataBound(object sender, ListViewItemEventArgs e)
{
    DataTable sourceData;

    sourceData = (DataTable)Listview1.DataSource;

    // sourceData is a DataTable, you can run .Compute or whatever you need
}

这篇关于如何访问在ASP.NET ListView的DataBound事件绑定数据源项目?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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