I have a ListView, which is bound with a list of 'A', which looks like this:
Class A
Property Id as Integer
Property TestStringA as String
Property B as B
End Class
Class B
Property Id as Integer
Property TestStringB as String
End Class
In the ListView i can refer to 'link property' values (whats the correct term for this?):
<asp:ListView runat="server" ID="lwTest" ItemType="A">
<LayoutTemplate>
<tr runat="server" id="itemPlaceHolder"></tr>
</LayoutTemplate>
<ItemTemplate>
<tr>
<td><%# Item.TestStringA %></td>
<td><%# Item.B.TestStringB %></td> (this is what I mean)
</tr>
</ItemTemplate>
</asp:ListView>
This is also working when using the Eval-method (if unable to use 'ItemType'):
<%# Eval(B.TestStringB) %>
I want to loop the ListView´s items and use the values from the container, without saving them in hidden fields (isn't that the purpose of 'DataKeyNames'?). The issue is, that I can not refer to a property of another object by it's link-attribute/property (in the example B.Id) in DataKeyNames. When I do like this, I get an error telling me that there´s no property called 'B.Id'):
<asp:ListView runat="server" ID="lwTest" ItemType="A" DataKeyNames"Id, B.Id">
<LayoutTemplate>
<tr runat="server" id="itemPlaceHolder"></tr>
</LayoutTemplate>
<ItemTemplate>
<tr>
<td><%# Item.TestStringA %></td>
<td><%# Item.B.TestStringB %></td> (this is what I mean)
</tr>
</ItemTemplate>
</asp:ListView>
Does anyone know if it is possible to do this? I know that I can add readonly properties returning the value directly to the bound object (but I would like to avoid this if possible):
Class A
Property Id as Integer
Property TestStringA as String
Property B as B
ReadOnly Property BId as Integer
Get
Return B.Id
End Get
End Property
End Class
Class B
Property Id as Integer
Property TestStringB as String
End Class
Thank you in advance!
First of all, you can only use direct properties of a class as your DataKeyNames. With the Eval instruction you can go deeper(but I believe only one level).
What you can do is change what is shown in your list view through the code-behind.
To do this, add an ItemDataBound event. The data for the item that is being created can be found in e.Item.DataItem end will be of type A (do cast it before using).
You should be able to access your controls directly through e.Item (don't know which properties to use by heart on this one) and once you've found the correct cell, you can set any text to it you want and since you have the bound instance of A itself you can call any property or sub property you want without restrictions.
This function gets called for every entry in your ListView, so they should only provide you data of the current entry.
Hope this helps you solve your issue.