在转发器中迭代复杂对象

时间:2014-06-11 22:18:10

标签: c# asp.net

我的转发器绑定了一个名为BillType的对象,在BillType对象中,有一个名为Vendors的另一个对象的列表。当我绑定到我的转发器时,我希望我的嵌套转发器打印出供应商。但我不知道如何遍历列表或者可能访问BillType中的供应商列表。有人可以轻松一点。感谢

//this is inside my .aspx nested Repeater
<%#                                                      
((HouseBudget.Business.BillType)Container.DataItem).VendorList.Count > 0 ?                           ((HouseBudget.Business.BillType)Container.DataItem).VendorList[0].VendorName : null %>

I can see there is Vendor information in there when I debug.
((HouseBudget.Business.BillType)Container.DataItem).VendorList
Count = 3
    [0]: {HouseBudget.Business.Vendor}
    [1]: {HouseBudget.Business.Vendor}
    [2]: {HouseBudget.Business.Vendor}

//This is how I can access one record, but I would like to write out all records
((HouseBudget.Business.BillType)Container.DataItem).VendorList[0].VendorName

1 个答案:

答案 0 :(得分:0)

使用嵌套转发器。这里重要的部分是将嵌套转发器的数据源绑定到外转发器的数据项的属性(DataSource='<%# VendorList %>'

<asp:Repeater ID='rptBillTypes' runat='server' DataSourceID='...'>
   <ItemTemplate>
     <asp:Literal ID='litClient' runat='server' Text='<%# Client %>' />

     <asp:Repeater ID='rptVendors' runat='server' DataSource='<%# VendorList %>'>
        <ItemTemplate>
           <asp:Literal ID='litVendorName' runat='server' Text='<%# VendorName %>' />           
        </ItemTemplate>
        <SeparatorTemplate>, </SeparatorTemplate>
     </asp:Repeater>

   </ItemTemplate>
</asp:Repeater>