带有实体框架的嵌套ASP.NET数据列表

本文关键字:ASP NET 数据 列表 嵌套 实体 框架 | 更新日期: 2023-09-27 18:30:09

我想使用实体框架显示两个嵌套有父子一元关系的数据列表。这就是我目前拥有的

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();

    }

我可以正确显示父数据列表,但我不太确定如何将数据源馈送到子数据列表中。

---编辑

我可以在父Datalist ItemDataBound事件上获取子Datalist的数据源:

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();
        }   
    }
}

如何将Committeeid从父DataListItem传递到事件?

带有实体框架的嵌套ASP.NET数据列表

没关系,我找到了

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();
        }   
    }
}