How to access the bound DataSource item in the ASP.NET ListView's DataBound event?

拥有回忆 提交于 2019-12-25 01:47:21

问题


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.


回答1:


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
}


来源:https://stackoverflow.com/questions/3769155/how-to-access-the-bound-datasource-item-in-the-asp-net-listviews-databound-even

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!