Dynamic Multi Level Menu Recursion in C#

Dynamic Multi Level Menu Recursion in C#
7 votes, 3.71 avg. rating (76% score)

Introduction

This is code snippet how to develop multi level menu with UL and LI structure. Thought to write simple one as it will help many web application developers to develop multi level menu(n level) very easily

Output

All you need to do it is just grab the free css and apply, code is here now

image

Categories Entity

 public class Categories
    {
        public int Id { get; set; }
        public string Name { get; set; }

        public int ParenetId { get; set; }
    }

DB Table will also have same structure. It can be menu table or categories table, everything is same (Id, Name, ParenttId). Item with no ParentId or ParenetId = 0 is the top most item

MenuItems

This will return the menu items with parent child relationship.

 
 public List<Categories> GetMenuItems()
        {
            List<Categories> MenuItmes = new List<Categories>();
            Categories item1 = new Categories { Id = 1, Name = "Item1" };
            Categories item2 = new Categories { Id = 2, Name = "Item2" };
            Categories item3 = new Categories { Id = 3, Name = "Item2_1", ParenetId=2 };
            Categories item4 = new Categories { Id = 4, Name = "Item2_2", ParenetId=2 };
            Categories item5 = new Categories { Id = 5, Name = "Item2_2_1", ParenetId = 4 };
            Categories item6 = new Categories { Id = 6, Name = "Item2_2_2", ParenetId = 4 };
            Categories item7 = new Categories { Id = 7, Name = "Item2_2_1_1", ParenetId = 5 };
            Categories item8 = new Categories { Id = 8, Name = "Item1_1", ParenetId = 1 };
            MenuItmes.Add(item1);
            MenuItmes.Add(item2);
            MenuItmes.Add(item3);
            MenuItmes.Add(item4);
            MenuItmes.Add(item5);
            MenuItmes.Add(item6);
            MenuItmes.Add(item7);
            MenuItmes.Add(item8);

            return MenuItmes;

        }

Creating Menu

 

AllMenuItems = GetMenuItems();

            List<Categories> parentItems = (from a in AllMenuItems where a.ParenetId == 0 select a).ToList();
            var ul = new HtmlGenericControl("ul");
            foreach(var parentcat in parentItems)
            {
                var li = new HtmlGenericControl("li");
               li.InnerText = parentcat.Name;
               ul.Controls.Add(li);
               List<Categories> childItems = (from a in AllMenuItems where a.ParenetId == parentcat.Id select a).ToList();
                if(childItems.Count > 0)
               li.Controls.Add(AddChildItem(parentcat, li));

            }

private HtmlGenericControl AddChildItem(Categories childItem, HtmlGenericControl pLi)
	{
	    var ul = new HtmlGenericControl("ul");
	    List<Categories> childItems = (from a in AllMenuItems where a.ParenetId == childItem.Id select a).ToList();
	    foreach (Categories cItem in childItems)
	    {
		var li = new HtmlGenericControl("li");
		li.InnerText = cItem.Name;
		List<Categories> subChilds = (from a in AllMenuItems where a.ParenetId == cItem.Id select a).ToList();
		if (subChilds.Count > 0)
		{
		    AddChildItem(cItem, li);
		}
		ul.Controls.Add(li);
	    }
	    pLi.Controls.Add(ul);
	    return ul;
	}
 

ul holds the complete menu

Output

image

Coclusion

It should be quite easy now to prepare your styled menus. Just find good styles and apply them. Dynamic generation is quite easy with the above code and works for any levels.
I use this method for my custom master page menu in SharePoint where I have list with different menu items. I also use this for menu in .Net applications.
Happy coding :)

May 17, 2015 В· Adi В· 2 Comments
Tags: , ,  В· Posted in: C#, Menu, Sharepoint 2007, Sharepoint 2010, SharePoint 2013

2 Responses

  1. Nagaraj - June 23, 2015

    Hi Team,
    how could we use this logic in situations like we don’t know how many items we have.. in this example we took 8 items so how about if we don’t know the items limit.

    reply would be much helpful.

    Thanks in Advance.

  2. Adi - June 23, 2015

    Hi Nagaraj,
    The method GetMenuItems is just for reference. Basically, in my project what I do is get the values from DB table and build the list of categories.No need to know the number of items prior.
    Hope this answers your query. Let me know if you want further help.
    Happy coding!
    Regards,
    Adi

Leave a Reply

What is 12 + 7 ?
Please leave these two fields as-is:
IMPORTANT! To be able to proceed, you need to solve the following simple math (so we know that you are a human) :-)