问题
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