I am in the process of switching over a control from a GridView to a ListView, but am stuck on accessing the new ListViews DataKeys. My ListView is setup as follows:
<asp:ListView ID="lvOrderItems" runat="server"
DataSourceID="odsShoppingCart"
DataKeyNames="ProductNumber"
ItemPlaceholderID="lvItemContainer">
Now, on Page_Load I need to do a few calculations, and need the ProductNumber for each ListView row. Here is how I am looping through the ListView:
For Each row As ListViewDataItem In lvOrderItems.Items
' Get the row id
Dim id As String = Convert.ToString(lvOrderItems.DataKeys(row.DisplayIndex).Value)
Response.Write("rowId=" & id)
Next
And I get the following output for my ListView that has two rows:
rowId=rowId=
So keep in mind, that when I had this as a GridView this was working no problem, and the ListView row labels that are bound directly to the datasouce in my page pull the data fine, so the data, including ProductNumber, is in there…
Any ideas what’s going wrong here?
Give this a shot. It’s in C#, but should be easy enough to port over to VB.NET