Solved

asp:repeater - menu - submenu

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

What Security Threats Are You Missing?

Enhance your security with threat intelligence from the web. Get trending threat insights on hackers, exploits, and suspicious IP addresses delivered to your inbox with our free Cyber Daily.

Join & Write a Comment

Just a quick little trick I learned recently.  Now that I'm using jQuery with abandon in my asp.net applications, I have grown tired of the following syntax:      (CODE) I suppose it just offends my sense of decency to put inline VBScript on a…
Calculating holidays and working days is a function that is often needed yet it is not one found within the Framework. This article presents one approach to building a working-day calculator for use in .NET.
Get a first impression of how PRTG looks and learn how it works.   This video is a short introduction to PRTG, as an initial overview or as a quick start for new PRTG users.
You have products, that come in variants and want to set different prices for them? Watch this micro tutorial that describes how to configure prices for Magento super attributes. Assigning simple products to configurable: We assigned simple products…

757 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

Need Help in Real-Time?

Connect with top rated Experts

21 Experts available now in Live!

Get 1:1 Help Now