开发者

ASP.NET Repeater - Display Property List<T1> of Databound List<T>

开发者 https://www.devze.com 2023-04-13 00:49 出处:网络
I have a list of objects in C# I\'m trying to bind to a Repeater.Here is a simplified example of my class:

I have a list of objects in C# I'm trying to bind to a Repeater. Here is a simplified example of my class:

public class LineDetail
{
    public int ClassID { get; set; }
    public IPerson Teacher { get; set; }
    public IList<IPerson> Students { get; set; }
    public string ClassTitle { get; set; }
    public DateTime StartTime { get; set; }
    public DateTime EndTime { get; set; }
}

The part that has be confused is the Students property. Of course, I bind my List to the repeater:

LineItems.Sort((x, y) => DateTime.Compare(x.StartTime, y.StartTime));
ResultsPanel.Visible = true;
Schedule.DataSource = Meetings;
Schedule.DataBind();

And I declare my repeater:

<asp:Repeater ID="Schedule" runat="server">
    <HeaderTemplate>
        <table width="650" border="1" bordercolor="#222222">
            <tr>
                <td> Time </td>
                <td> Teacher </td>
                <td> Class Title </td>
                <td> Students </td>
            </tr>
    </HeaderTemplate>
    <ItemTemplate>
        <tr>
            <td>
                <%# Convert.ToDateTime(DataBinder.Eval(Container.DataItem, "StartTime")).ToString("h:mm")%>
                -
                <%# Convert.ToDateTime(DataBinder.Eval(Container.DataItem, "EndTime")).ToString("h:mm")%>
            </td>
            <td> <%# DataBinder.Eval(Container.DataItem, "Teacher").ToString() %> </td>
            <td> <%# DataBinder.Eval(Container.DataItem, "ClassTitle")%> </td>
            <td> <%# DataBinder.Eval(Container.DataItem, "Students")%> </td>
        </tr>
    </ItemTemplate>
    <FooterTemplate></table></FooterTemplate>
</asp:Repeater>

But I don't know how (or if its even possible) to output the List Students property. I tried a nested repeater, but that didn't work. I don't believe it was syntax related since it compiled.

My inner repeater was:

<td> 
    <asp:Repeater id="ParliamentarianRepeater" runat="server">
        <HeaderTemplate><ul></HeaderTemplate>
        <ItemTemplate>
            <l开发者_如何学JAVAi>
                <%# DataBinder.Eval(Container.DataItem, "Students.Firstname") %>
            </li>
        </ItemTemplate>
        <FooterTemplate></ul></FooterTemplate>
    </asp:Repeater>
</td>

I don't know how to accomplish what I need to do.

Can you please point me in the right direction? I'm open to other ways in which this data could be displayed nicely as well. Thanks in advance!


You have to set the DataSource for the Inner Repeater

Your inner repeater code should look like this

<td><asp:Repeater id="ParliamentarianRepeater" runat="server" DataSource='<%# DataBinder.Eval(Container.DataItem, "Students")%>'>
<HeaderTemplate><ul></HeaderTemplate>
<FooterTemplate></ul></FooterTemplate>
<ItemTemplate>
<li>
<%# DataBinder.Eval(Container.DataItem, "Firstname") %>
</li>
</ItemTemplate>
</asp:Repeater>
</td>

The inner repeater must be inside the "ItemTemplate" tag from your parent Repeater.

Hope this helps

0

精彩评论

暂无评论...
验证码 换一张
取 消