程序化嵌套列表

时间:2018-08-13 12:51:14

标签: c# asp.net listview repeater

我正在尝试使用ListView控件在Asp.Net/C#中创建一个嵌套列表。我看了很多例子,但似乎对它们毫无意义。

这是模式:

<ul>
    <li>Item 1 - level 1</li>
    <li>Item 2 - level 1</li>
    <li>Item 3 - level 1
        <ul>
            <li>Item 1 - level 2</li>
            <li>Item 2 - level 2</li>
            <li>Item 3 - level 2</li>
        </ul>
    </li>
</ul>

我创建了一个列表,该列表使用单个级别,因此它与html的嵌套方式不正确-只是<li>标签的平面列表。

但是,我想进行更改。

只有一个级别有一个子菜单-可能某些级别1的项目将没有任何子项目-所以我正在寻找一种可以呈现0或1个子菜单的模式。

任何人都知道我需要使用以下代码来实现我想要的吗?

<asp:ListView ID="lv" runat="server"
    OnItemDataBound="LV_ItemDataBound">
    <LayoutTemplate>
        <nav>
            <ul class="content-nav">
                <asp:PlaceHolder ID="itemPlaceholder" runat="server" />
            </ul>
        </nav>
    </LayoutTemplate>
    <ItemTemplate>
        <li id="liMenuItem" runat="server">
            <asp:HyperLink ID="lnkMenuItem" runat="server"
            CssClass="content-nav_link"></asp:HyperLink>
        </li>
    </ItemTemplate>
</asp:ListView>

        protected void LV_ItemDataBound(object source, ListViewItemEventArgs e)
        {
            var item = e.Item;

            if (item.ItemType == ListViewItemType.DataItem)
            {
                var data = (ContentNavItem)item.DataItem;

                var liMenuItem = item.GetControl<HtmlGenericControl>("liMenuItem");

                // Do something with the item here
            }
        }

void Build()
{
    var currentId = MenuItems.First(x => x.Route == CurrentUrl).Id;

    var currentItems = MenuItems
        .Where(x => x.IsTopLevel || x.ParentId == currentId)
        .OrderBy(x => x.GroupId).ThenBy(x => x.Anchor);

    lv.DataSource = currentItems;
    lv.DataBind();
}

1 个答案:

答案 0 :(得分:0)

将菜单创建为带有StringBuilder的字符串会容易得多。但是,如果您确实需要递归控件,则可以克隆该控件,并将其重新用于每个菜单级别。我在此演示中使用了Repeater,因为它更适合于重复项目。所以首先我们创建一个类和一些演示数据

public class MenuItem
{
    public string name { get; set; }
    public List<MenuItem> menuitems { get; set; }
}

用虚拟数据填充

protected void Page_Load(object sender, EventArgs e)
{
    List<MenuItem> MenuItems = new List<MenuItem>();

    for (int i = 0; i < 10; i++)
    {
        MenuItems.Add(new MenuItem() { name = "Name " + i });

        if (i == 2 || i == 5 || i == 8)
        {
            MenuItems[i].menuitems = new List<MenuItem>();
            MenuItems[i].menuitems.Add(new MenuItem() { name = "SubName 1" });
            MenuItems[i].menuitems.Add(new MenuItem() { name = "SubName 2" });
            MenuItems[i].menuitems.Add(new MenuItem() { name = "SubName 3" });

            if (i == 8)
            {
                MenuItems[i].menuitems[0].menuitems = new List<MenuItem>();
                MenuItems[i].menuitems[0].menuitems.Add(new MenuItem() { name = "SubName 1" });
                MenuItems[i].menuitems[0].menuitems.Add(new MenuItem() { name = "SubName 2" });
                MenuItems[i].menuitems[0].menuitems.Add(new MenuItem() { name = "SubName 3" });
            }
        }
    }

    Repeater1.DataSource = MenuItems;
    Repeater1.DataBind();
}

将Repeater控件添加到页面。还可以使用PlaceHolder,以便它可以保存其自身的副本。

<nav>
    <asp:Repeater ID="Repeater1" runat="server" OnItemDataBound="Repeater1_ItemDataBound">
        <HeaderTemplate>
            <ul class="content-nav">
        </HeaderTemplate>
        <ItemTemplate>
            <li id="liMenuItem" runat="server">
                <%# Eval("name") %>
                <asp:PlaceHolder ID="PlaceHolder1" runat="server"></asp:PlaceHolder>
            </li>
        </ItemTemplate>
        <FooterTemplate>
            </ul>
        </FooterTemplate>
    </asp:Repeater>
</nav>

现在在ItemDataBound事件中,我们克隆Repeater1,将其填充到子菜单中,并将其添加到父Repeater的PlaceHolder中,并附加ItemDataBound程序,以便Repeater可以生成其自己的子代。 / p>

protected void Repeater1_ItemDataBound(object sender, RepeaterItemEventArgs e)
{
    MenuItem item = e.Item.DataItem as MenuItem;

    //check if the item exists and if it has subitems
    if (item != null && item.menuitems != null)
    {
        //determine type and get the properties
        Type type = sender.GetType();
        PropertyInfo[] properties = type.GetProperties();
        Object obj = type.InvokeMember("", BindingFlags.CreateInstance, null, sender, null);

        //copy the properties
        foreach (PropertyInfo propertyInfo in properties)
        {
            if (propertyInfo.CanWrite)
            {
                propertyInfo.SetValue(obj, propertyInfo.GetValue(sender, null), null);
            }
        }

        //cast the created object back to a repeater
        Repeater nestedRepeater = obj as Repeater;

        //fill the child repeater with the sub menu items
        nestedRepeater.DataSource = item.menuitems;

        //attach the itemdatabound event
        nestedRepeater.ItemDataBound += Repeater1_ItemDataBound;

        //bind the data
        nestedRepeater.DataBind();

        //find the placeholder and add the created Repeater
        PlaceHolder ph = e.Item.FindControl("PlaceHolder1") as PlaceHolder;
        ph.Controls.Add(nestedRepeater);
    }
}