views:

34

answers:

1

I need to generate a table from a List(Of Students). My Student class has properties for AcademicYear, TeachingSet, Surname and Forenames, is sorted in that order and also properties for ID and start date. The table should nest TeachingSets within AcademicYears and then the students within the TeachingSets, as shown in the table I've mocked up at http://www.ifslearning.ac.uk/files/student-table.jpg

Using a repeater I get
08-10 students B74394 Mzejb Bsppn
08-10 students B74395 Lbuifsjof Bvti
08-10 students C68924 Epoob Cmpblf
08-10 students D41468 Ipxbse Dbwfz

But I need to have
08-10 students
- B74394 Mzejb Bsppn
- B74395 Lbuifsjof Bvti
- C68924 Epoob Cmpblf
- D41468 Ipxbse Dbwfz

A: 

If you use asp .net Web Forms then you can look at ListView, DataList and Repeater controls. I would recommend you to try ListView. Here is a deep description about using this control. It is very simple to configure any layout you need:

<asp:ListView runat="server" ID="ListView1">
    <LayoutTemplate>
        <table>
            <tr runat="server" ID="itemPlaceholder" />
        </table>
    </LayoutTemplate>
    <ItemTemplate>
        <tr>
            <td><%#Eval("YourObjectProp") %></td>
            ...
        </td>
    </ItemTemplate>
</asp:ListView>

You can group your objects like this:

List<AcademicYear> academicYears = new List<AcademicYear>();
var year = new AcademicYear { Year = "08-10" };
year.Students.Add(new Student { Name = "Mzejb Bsppn" });
year.Students.Add(new Student { Name = "Lbuifsjof Bvti" });
...

And layout:

<asp:ListView runat="server" ID="ListView1">
    <LayoutTemplate>

            <asp:PlaceHolder runat="server" ID="itemPlaceholder" />

    </LayoutTemplate>
    <ItemTemplate>
        <%#Eval("Year") %> students<br />
         <asp:ListView runat="server" ID="nestedListView" DataSource='<%# Eval("Students") %>'>
            <LayoutTemplate>

               <asp:PlaceHolder runat="server" ID="itemPlaceholder"  />

            </LayoutTemplate>
            <ItemTemplate>
               <%#Eval("Name") %><br />

            </ItemTemplate>
        </asp:ListView>   
    </ItemTemplate>
</asp:ListView>
Tim
Thanks for the reply Tim. Yes I am using Web Forms. I can get everything out on the page using a repeater control - but I get a row per student this way and I need the grouping by AcademicYear then TeachingSet... that's the bit I'm stuck on.
Simon Martin
You are welcome. You need to group objects on your business model appropriately and use nested ListView or Repeater control. I edited the post with example
Tim
The analyst has determined the model for the Student class and methods to retrieve the list. So when the page loads I call Process.Students and there's my List(Of Students). If I understand you correctly then what you're saying is that I should split that up into page level object variables? So a List(Of AcademicYears) which contains a List(Of TeachingSets) which contains a List(Of Students) and then DataBind my top level ListView / Repeater to the page variable List(Of AcademicYears) rather than my Process.Students, then nested controls to iterate through each sub collection?
Simon Martin
Yes, you can do it with variable or with LINQ expression: http://msdn.microsoft.com/en-us/vcsharp/aa336754.aspx#nested
Tim
Hi Tim - thought I'd update you on my progress with this. I decompressed Process.Student into a couple of structures, so I could keep named Properties which were required for the Evals - but have got that working now. Many thanks for your help
Simon Martin