views:

29

answers:

1

I want to create something like this using a DataList and Flow markup:

|-----------------------|
|        Title          |
|-----------------------|
| [x] Title             |
| [x] Title             |
| ...                   |
-------------------------

I have a table (modeled in Linq2Sql) Foo that has these fields

int id;
int? parentId;
string title;
Foo Parent;
EntitySet<Foo> Children;

Now, when there is a null parent, it means it's a top level category, and if the parent has a value, it's part of the category list.

I have created a DataList, and used a LinqDataSource with a query that looks like this:

<asp:LinqDataSource ID="LinqDataSource1" runat="server" 
    ContextTypeName="MyNameSpace.FooDataContext" 
    Select="new (Title, Children)" TableName="Foo" 
    Where="ParentID = NULL">
</asp:LinqDataSource>

<asp:DataList ID="FooList" runat="server" DataSourceID="LinqDataSource1" 
  BorderColor="Black" BorderWidth="1px" RepeatLayout="Flow">
  <ItemTemplate>
    <asp:Label ID="TitleLabel" runat="server" Text='<%# Eval("Title") %>' />
    <br />
    <asp:Repeater ID="Repeater1" runat="server" DataSourceID="<%# Eval("Children") %>">
        <ItemTemplate>
            <asp:CheckBox ID="Checks" Text="<%# Eval("Title") %>" runat="server" />
        </ItemTemplate>
    </asp:Repeater>
  </ItemTemplate>
</asp:DataList>

This obviously doesn't work. How can I utilize the Children collection in a repeater of a DataList item?

+1  A: 

I tried this with my schema tables and it works OK.The trick is to use the ItemDataBound event of the Datalist.See markup below.Note that I am using Scool entity which has a collection of Teacher entities

<asp:DataList ID="DataList1" runat="server" DataKeyField="id" 
    DataSourceID="LinqDataSource1" Width="246px" 
    onitemdatabound="DataList1_ItemDataBound">
    <ItemTemplate>
        name:
        <asp:Label ID="nameLabel" runat="server" Text='<%# Eval("name") %>' />
        <div style="border:solid blue 3px;padding:2px;">
       <asp:Repeater ID="rptteachers" runat="server"  >
       <ItemTemplate>
    <asp:Label ID="kllj" runat="server" Text='<%# Eval("name") %>'  ></asp:Label>
    <asp:Label ID="Label1" runat="server" Text='<%# Eval("quals") %>'  ></asp:Label> 
     </ItemTemplate>
       </asp:Repeater>
       </div>
    </ItemTemplate>
</asp:DataList>

Then you add the following to the code behind.

protected void DataList1_ItemDataBound(object sender, DataListItemEventArgs e)
    {

        Repeater r = e.Item.FindControl("rptteachers") as Repeater;
        if (r == null) return;
        if (e.Item.ItemType == ListItemType.Item || e.Item.ItemType ==        ListItemType.AlternatingItem)
        {
            school sc = e.Item.DataItem as school;
            if (sc == null) return;
            r.DataSource = sc.Teachers;
            r.DataBind();
        }
    }
josephj1989
Yeah, this was the solution I basically came up with as well. In fact, it's almost identical.
Mystere Man