Simon Martin Simon Martin - 3 days ago 5
Vb.net Question

Create table from a business object with conditional layout

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

Answer

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>
Comments