次のような「A」のリストにバインドされたListViewがあります。
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
前もって感謝します!