0

我想使用实体框架显示 2 个嵌套在父子一对多关系中的数据列表。这是我到目前为止所拥有的

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 传递给事件?

4

1 回答 1

0

没关系,我想通了

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();
        }   
    }
}
于 2013-09-04T22:07:11.437 回答