• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 2260
  • Last Modified:

asp:repeater - menu - submenu

Hello,
I would like to create a menu with submenuitems like this using asp:Repeater.
<ul>
			<li id="li1" runat="server">Menu 1
				<ul>
					<li>Submenu1</li>
					<li>Submenu2</li>
				</ul>
			</li>
			<li id="li2" runat="server">Menu 2</li>
		</ul>

Open in new window


Here below you see what I have done so far. This creates a normal menu. I do not know how to add the submenu. I appreciate your help

<asp:Repeater ID="rptTest" runat="server">
			<HeaderTemplate>
				<ul>
			</HeaderTemplate>
			<ItemTemplate>
				<%#Eval("FullName") %><li>Value
				</li>
			</ItemTemplate>
			<FooterTemplate>
				</ul>
			</FooterTemplate>
		</asp:Repeater>

Open in new window


In code behind I bind to it like this

protected void Page_Load(object sender, EventArgs e)
    {
		List<User> iList = new List<User>();
		User i = new User();
		i.FullName = "MenuItem1";
		iList.Add(i);
		User i2 = new User();
		i2.FullName = "MenuItem2";
		iList.Add(i2);
		rptTest.DataSource = iList;
		rptTest.DataBind();
    }

Open in new window

0
johnkainn
Asked:
johnkainn
  • 2
1 Solution
 
Rajar AhmedConsultantCommented:
Try this,
<%@ Page Language="C#" AutoEventWireup="true" CodeFile="repeaterULFil.aspx.cs" Inherits="repeaterULFil" %>

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">

<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
    <title></title>
</head>
<body>
    <form id="form1" runat="server">
    <div>
    
    </div>
    <asp:Repeater ID="rptTest" runat="server" OnItemDataBound="rptTest_ItemDataBound">
        <HeaderTemplate>
            <ul>
        </HeaderTemplate>
        <ItemTemplate>
            <li>
                <%#Eval("FullName") %>
                <asp:Repeater ID="rptTest1" runat="server" >
                    <HeaderTemplate>
                        <ul>
                    </HeaderTemplate>
                    <ItemTemplate>
                        <li>
                            <%#Eval("FullName") %></li>
                    </ItemTemplate>
                    <FooterTemplate>
                        </ul>
                    </FooterTemplate>
                </asp:Repeater>
            </li>
        </ItemTemplate>
        <FooterTemplate>
            </ul>
        </FooterTemplate>
    </asp:Repeater>
    </form>
</body>
</html>

Open in new window

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Data;

public partial class repeaterULFil : System.Web.UI.Page
{
    protected void Page_Load(object sender, EventArgs e)
    {
        rptTest.DataSource = ds();
        rptTest.DataBind();

       
    }

    protected void rptTest_ItemDataBound(object sender, RepeaterItemEventArgs e)
    {
        if (e.Item.ItemType == ListItemType.Item || e.Item.ItemType == ListItemType.AlternatingItem)
        {
            Repeater rptrSubSubCategory = (Repeater)e.Item.FindControl("rptTest1");
            rptrSubSubCategory.DataSource = ds();
            rptrSubSubCategory.DataBind();
        }
    }
     List<User> ds()
    {
        List<User> iList = new List<User>();
        User i = new User();
        i.FullName = "MenuItem1";
        iList.Add(i);
        User i2 = new User();
        i2.FullName = "MenuItem2";
        iList.Add(i2);
        User i3 = new User();
        i3.FullName = "MenuItem3";
        iList.Add(i3);
        User i4 = new User();
        i4.FullName = "MenuItem4";
        iList.Add(i4);
        return iList;
    }
    class User {
        private string myName;
        public string FullName
        {
            get
            {
                return myName;
            }
            set
            {
                myName = value;
            }
        }

    }
}

Open in new window

0
 
johnkainnAuthor Commented:
Thank you. Some items will have sub menus but others not. Is it possible that you send a demo how I would get following output

Menu1
     Submenu1
     Submenu2
Menu 2 (no submenu)

or like this:

<ul>
                  <li id="li1" runat="server">Menu 1
                        <ul>
                              <li>Submenu1</li>
                              <li>Submenu2</li>
                        </ul>
                  </li>
                  <li id="li2" runat="server">Menu 2</li>
            </ul>
0
 
Rajar AhmedConsultantCommented:
yes, possible, i hope the child repeater is based on parent repearer key. On itemdatabound event you can configure .
0

Featured Post

NFR key for Veeam Backup for Microsoft Office 365

Veeam is happy to provide a free NFR license (for 1 year, up to 10 users). This license allows for the non‑production use of Veeam Backup for Microsoft Office 365 in your home lab without any feature limitations.

  • 2
Tackle projects and never again get stuck behind a technical roadblock.
Join Now