Solved

asp:repeater - menu - submenu

Posted on 2013-01-21
3
1,998 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

MS Dynamics Made Instantly Simpler

Make Your Microsoft Dynamics Investment Count  & Drastically Decrease Training Time by Providing Intuitive Step-By-Step WalkThru Tutorials.

Question has a verified solution.

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

Introduction This article shows how to use the open source plupload control to upload multiple images. The images are resized on the client side before uploading and the upload is done in chunks. Background I had to provide a way for user…
This article shows how to deploy dynamic backgrounds to computers depending on the aspect ratio of display
With Secure Portal Encryption, the recipient is sent a link to their email address directing them to the email laundry delivery page. From there, the recipient will be required to enter a user name and password to enter the page. Once the recipient …

726 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