简体   繁体   English

ListView DataKeys 未设置/传递

[英]ListView DataKeys Not Being Set/Passed

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.我正在将控件从GridViewListView ,但我无法访问新的 ListViews DataKeys。 My ListView is setup as follows:我的 ListView 设置如下:

<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.现在,在 Page_Load 上我需要做一些计算,并且需要每个ListView行的 ProductNumber。 Here is how I am looping through the ListView :这是我循环遍历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:对于有两行的 ListView,我得到以下 output:

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...所以请记住,当我将它作为 GridView 时,这没有问题,并且直接绑定到我页面中数据源的 ListView 行标签可以很好地提取数据,因此包括 ProductNumber 在内的数据就在那里。 ..

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它在 C# 中,但应该很容易移植到 VB.NET

string productName = (string)lvOrderItems.DataKeys[row.DisplayIndex]["ProductNumber"];

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM