Solved

asp:repeater - menu - submenu

Posted on 2013-01-21
3
1,940 Views
Last Modified: 2013-01-24
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
Comment
Question by:johnkainn
  • 2
3 Comments
 
LVL 18

Expert Comment

by:Rajar Ahmed
ID: 38802033
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
 

Author Comment

by:johnkainn
ID: 38802186
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
 
LVL 18

Accepted Solution

by:
Rajar Ahmed earned 500 total points
ID: 38803984
yes, possible, i hope the child repeater is based on parent repearer key. On itemdatabound event you can configure .
0

Featured Post

Master Your Team's Linux and Cloud Stack!

The average business loses $13.5M per year to ineffective training (per 1,000 employees). Keep ahead of the competition and combine in-person quality with online cost and flexibility by training with Linux Academy.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

It was really hard time for me to get the understanding of Delegates in C#. I went through many websites and articles but I found them very clumsy. After going through those sites, I noted down the points in a easy way so here I am sharing that unde…
International Data Corporation (IDC) prognosticates that before the current the year gets over disbursing on IT framework products to be sent in cloud environs will be $37.1B.
Two types of users will appreciate AOMEI Backupper Pro: 1 - Those with PCIe drives (and haven't found cloning software that works on them). 2 - Those who want a fast clone of their boot drive (no re-boots needed) and it can clone your drive wh…

832 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question