我有一个 ListView,它与“A”列表绑定,如下所示:
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
在 ListView 中,我可以参考“链接属性”值(正确的术语是什么?):
<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>
这在使用 Eval 方法时也有效(如果无法使用“ItemType”):
<%# Eval(B.TestStringB) %>
我想循环 ListView 的项目并使用容器中的值,而不将它们保存在隐藏字段中(这不是“DataKeyNames”的目的吗?)。问题是,我不能通过 DataKeyNames 中的链接属性/属性(在示例 B.Id 中)来引用另一个对象的属性。当我这样做时,我收到一条错误消息,告诉我没有名为“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>
有谁知道是否可以这样做?我知道我可以添加将值直接返回到绑定对象的只读属性(但如果可能的话,我想避免这种情况):
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
先感谢您!