Question

I want to display 2 Datalists nested with a parent-child 1-many relationship using Entity Framework. This is what I have so far

ASP.NET

<asp:DataList ID="dlRange" runat="server" RepeatDirection="Vertical" 
        RepeatLayout="Flow" onitemdatabound="dlRange_ItemDataBound">
<ItemTemplate>
    <hr><h2>
    <%# Eval("COMMITEENAME")%>
    </h2>
    <asp:DataList ID="dlComp" runat="server" RepeatDirection="Vertical" RepeatLayout="Flow" DataSource='<%# Eval("comp") %>'>
    <ItemTemplate>
        <label for='<%#Eval("CID")%>' ><%# Eval("NAME")%></label>
        <input id='<%#Eval("CID")%>' type="range" pattern="[0-9]*" name='<%#Eval("CID")%>' min="0" max="10" data-highlight="true" value="" data-show-value="true" data-popup-enabled="true" />
    </ItemTemplate>
    </asp:DataList>
</ItemTemplate>
</asp:DataList>

CS

using (BOTEntities context = new BOTEntities())
    {
        var data = (from com in context.COMMITEETYPEs
                   join comComp in context.COMMITTEECOMPs on com.COMMITEETYPEID equals comComp.COMMITTEEID
                   where com.ACTIVE == 1
                   select new { com.COMMITEENAME, comComp.COMMITTEEID }).Distinct().OrderBy(a => a.COMMITEENAME).ToList();


        for (int i = 0; i <= data.Count; i++)
        {
            decimal comid = (decimal)data[i].COMMITTEEID;
            var comps = (from comp in context.COMPETENCies
                         join comComp in context.COMMITTEECOMPs on comp.CID equals comComp.CID
                         where comComp.COMMITTEEID == comid
                         orderby comp.NAME
                         select new { comp.CID, comp.NAME }).ToList();
        }

        dlRange.DataSource = data;
        dlRange.DataBind();


    }

I can get the Parent Datalist displays correctly but I'm not quite sure how to feed the datasource into the child Datalist.

---Edit

I can fetch the Datasource for the child Datalist on the parent Datalist ItemDataBound event:

protected void dlRange_ItemDataBound(object sender, DataListItemEventArgs e)
{
    if (e.Item.ItemType == ListItemType.Item || e.Item.ItemType == ListItemType.AlternatingItem)
    {
        DataList childDL = e.Item.FindControl("dlComp") as DataList;

        using (BOTEntities context = new BOTEntities())
        {
            var comps = (from comp in context.COMPETENCies
                         join comComp in context.COMMITTEECOMPs on comp.CID equals comComp.CID
                         where comComp.COMMITTEEID == 1 //hardcoded to test
                         orderby comp.NAME
                         select new { comp.CID, comp.NAME }).ToList();

            childDL.DataSource = comps;
            childDL.DataBind();
        }   
    }
}

How do I pass the Committeeid from the parent DataListItem to the event?

Was it helpful?

Solution

Nevermind, I figured it out

protected void dlRange_ItemDataBound(object sender, DataListItemEventArgs e)
{
    if (e.Item.ItemType == ListItemType.Item || e.Item.ItemType == ListItemType.AlternatingItem)
    {
        DataList childDL = e.Item.FindControl("dlComp") as DataList;
        var item = e.Item;
        var dataItem = item.DataItem;
        decimal comID = (decimal)DataBinder.Eval(dataItem, "COMMITTEEID");
        using (BOTEntities context = new BOTEntities())
        {
            var comps = (from comp in context.COMPETENCies
                         join comComp in context.COMMITTEECOMPs on comp.CID equals comComp.CID
                         where comComp.COMMITTEEID == comID
                         orderby comp.NAME
                         select new { comp.CID, comp.NAME }).ToList();

            childDL.DataSource = comps;
            childDL.DataBind();
        }   
    }
}
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top