aspx:ListView中的ListView,带有来自数据库的ID

时间:2017-07-13 09:24:13

标签: asp.net database listview

我是aspx的初学者,但我想在里面使用ListView和ListView。第一个包含在内的多个ListView将从数据库中获取其ID。我有多个问题,但首先:它有可能吗?如果是,有人可以解释我的代码如何以及为什么不起作用?这是我的aspx代码:

<asp:ListView ID="gvBureaux" runat="server"> <!-- first ListView -->
            <ItemTemplate>
                <div class="content__item" data-space='<%# Eval("Space_Bureau") %>' data-category='<%# Eval("Category_Bureau") %>'> <!-- this works-->
                    <h3 class="content__item-title"><%# Eval("Label_Bureau") %></h3> <!-- this works-->
                    <asp:ListView ID='<%# Eval("Code_Bureau") %>' runat="server"> <!-- error -->
                        <ItemTemplate>
                            <!-- this works-->
                            <div class="col-md-2">
                                <div class="product">
                                    <div class="product__info">
                                        <img class="product__image" src='<%# Eval("Photo") %>' alt="Product 1" />
                                        <a href='skype:<%# Eval("Coll_Skype") %>?chat' class='<%# Eval("Coll_Skype") != null ? "" : "hide" %>'><i class="fa fa-lg fa-skype"></i></a>
                                        <a href='mailto:<%# Eval("Coll_Mail") %>' class='<%# Eval("Coll_Mail") != null ? "" : "hide" %>'><i class="fa fa-lg fa-envelope-o"></i></a>
                                        <h3 class="product__title"><%# Eval("NomPrenomCapitalize") %></h3>
                                        <span class="product__price highlight"><%# Eval("CompetencesTrombi") %></span>
                                        <i class='fa fa-lg fa-phone'><%# Eval("Coll_Fixe") %></i>
                                        <i class='fa fa-lg fa-mobile-phone'><%# Eval("Coll_Port") %></i>
                                    </div>
                                </div>
                            </div>
                            <!-- /this works-->
                        </ItemTemplate>
                    </asp:ListView>
                </div>
            </ItemTemplate>
        </asp:ListView>

我的代码背后:

gvBureaux.DataSource = M_Bureau.GetListeBureau();
        gvBureaux.DataBind();
        var Bureaux = M_Bureau.GetListeBureau();
        ListView listView = new ListView();

        foreach (Bureau Br in Bureaux)
        {
            var collaborateurs = M_Collaborateur.GetCollaborateursByidBureau(Br.ID_Bureau);
            listView.DataSource = collaborateurs;
            listView.DataBind();
        }

谢谢!

1 个答案:

答案 0 :(得分:0)

以下是使用ItemDataBound事件构建嵌套列表的解决方案。

我使用一些数据结构和生成器进行演示,根据需要用查询替换它们:

public class InnerListItem
{
    public string InnerItemTitle { get; set; }
}

public class OuterListItem
{
    public int Id { get; set; }
    public string ListTitle { get; set; }
}

private List<OuterListItem> GetOuterList(int listsCount)
{
    var result = new List<OuterListItem>();
    for (int i = 0; i < listsCount; i++)
    {
        result.Add(new OuterListItem
        {
            Id = i + 1,
            ListTitle = string.Format("List {0}", i + 1)
        });
    }
    return result;
}
private List<InnerListItem> GetInnerList(int id, int innerItemCount)
{
    var result = new List<InnerListItem>();
    for (int i = 0; i < innerItemCount; i++)
    {
        result.Add(new InnerListItem
        {
            InnerItemTitle = string.Format("Item {0}.{1}", id, i + 1)
        });
    }
    return result;
}

ASPX示例:

<asp:ListView ID="outerList" OnItemDataBound="outerList_ItemDataBound" runat="server">
    <LayoutTemplate>
        <ul>
            <li id="itemPlaceholder" runat="server" />
        </ul>
    </LayoutTemplate>
    <ItemTemplate>
        <li>
            <label><%# Eval("ListTitle") %></label>
            <asp:ListView ID="innerList" runat="server">
                <LayoutTemplate>
                    <ol>
                        <li id="itemPlaceholder" runat="server" />
                    </ol>
                </LayoutTemplate>
                <ItemTemplate>
                    <li>
                        <span><%# Eval("InnerItemTitle") %></span>
                    </li>
                </ItemTemplate>
            </asp:ListView>
        </li>
    </ItemTemplate>
</asp:ListView>

启动内部列表绑定的初始化和事件函数

protected void Page_Init(object sender, EventArgs e)
{
    var outerItems = GetOuterList(2);
    outerList.DataSource = outerItems;
    outerList.DataBind();
}

protected void outerList_ItemDataBound(object sender, ListViewItemEventArgs e)
{
    if (e.Item.ItemType == ListViewItemType.DataItem)
    {
        var innerList = e.Item.FindControl("innerList") as ListView;
        var listObject = e.Item.DataItem as OuterListItem;
        var innerItems = GetInnerList(listObject.Id, 3);
        innerList.DataSource = innerItems;
        innerList.DataBind();
    }
}

应该可以根据实际数据情况转换样本。