Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 6892259
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 27, 20262026-05-27T06:30:45+00:00 2026-05-27T06:30:45+00:00

I am currently using an asp.net menu control to load from a table parent/child

  • 0

I am currently using an asp.net menu control to load from a table parent/child items. The problem I am having is that if the child has another child. My code is kindof static in that sense and I can’t seem to find a better or “the” way to do it. I have seen sitemap as datasources but i don’t need a sitemap and feel that would just be overkill for what I need to achieve.

foreach (ClassName option in list)
{
   MenuItem module = new MenuItem(option.Description.ToLower(), "", "", option.Url + "?option=" + option.Optionid);
   module.Selectable = true;
   navigation.Items.Add(module);
   //this is my second level
   foreach (ClassName child in listfromparent(option.Optionid))
   {
         MenuItem childmenu = new MenuItem(child.Description.ToLower(), "", "", child.Url + "?option=" + child.Optionid);
         module.ChildItems.Add(childmenu);
   }
 }

as you can see this works but for 2 levels 🙁
and of course i could put another childlevel inside child to create the 3rd level but what if there is a 4th, 5th? So that’s why I need it to do it itself. I noticed treeview has onpopulate but apparently Menu doesn’t. Thanks in advance.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-27T06:30:46+00:00Added an answer on May 27, 2026 at 6:30 am

    Here’s one way you could do it.

    • Represent parent/child relationship in your table with an adjacency list
    • Map that adjacency list into a tree structure
    • Convert that tree structure into your structure of menu items

    Maybe you could skip that middle step and map the adjacency list straight to a tree of MenuItems, maybe with some extension methods on MenuItem.

    But anyway…

    Default.aspx

    <%@ Page Language="C#" Inherits="MenuTreeDemo.Default" %>
    <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
    <html>
    <head runat="server">
        <title>Default</title>
    </head>
    <body>
        <form id="form1" runat="server">
            <asp:Menu ID="MyMenu" runat="server" StaticDisplayLevels="3" />
        </form>
    </body>
    </html>
    

    Default.aspx.cs

    using System;
    using System.Web;
    using System.Web.UI;
    using System.Web.UI.WebControls;
    using System.Data;
    using System.Collections.Generic;
    
    namespace MenuTreeDemo
    {
        public partial class Default : System.Web.UI.Page
        {
            protected void Page_Load(object sender, EventArgs e)    
            {
                if (!IsPostBack)
                {
                    MenuNode root = ConvertTableToTree(GetTreeTable());
                    foreach (MenuNode topLevelNode in root.Children)
                    {
                        MyMenu.Items.Add(topLevelNode.ToMenuItem()); // Visits all nodes in the tree.
                    }
                }
            }
    
    
            // The menu tree as an adjacency list in a table. 
            static DataTable GetTreeTable()
            {
                DataTable table = new DataTable();
                table.Columns.Add("Id", typeof(int));
                table.Columns.Add("Description", typeof(string));
                table.Columns.Add("Url", typeof(string));
                table.Columns.Add("ParentId", typeof(int));
    
                table.Rows.Add(1, "TopMenu1", "/foo.html", 0);
                table.Rows.Add(2, "SubMenu1.1", "/baz.html", 1);
                table.Rows.Add(3, "SubMenu1.2", "/barry.html", 1);
                table.Rows.Add(4, "SubMenu1.2.1", "/skeet.html", 3);
                table.Rows.Add(5, "TopMenu2", "/bar.html", 0);
                table.Rows.Add(6, "TopMenu3", "/bar.html", 0);
                table.Rows.Add(7, "SubMenu3.1", "/ack.html", 6);
    
                return table;
            }
    
    
            // See e.g. http://stackoverflow.com/questions/2654627/most-efficient-way-of-creating-tree-from-adjacency-list
            // Assuming table is ordered.
            static MenuNode ConvertTableToTree(DataTable table)
            {
                var map = new Dictionary<int, MenuNode>();
                map[0] = new MenuNode() { Id = 0 }; // root node
    
                foreach (DataRow row in table.Rows)
                {
                    int nodeId = int.Parse(row["Id"].ToString());
                    int parentId = int.Parse(row["ParentId"].ToString());
    
                    MenuNode newNode = MenuNodeFromDataRow(row);
    
                    map[parentId].Children.Add(newNode);
                    map[nodeId] = newNode;
                }
    
                return map[0]; // root node
            }
    
    
            static MenuNode MenuNodeFromDataRow(DataRow row)
            {
                int nodeId = int.Parse(row["Id"].ToString());
                int parentId = int.Parse(row["ParentId"].ToString());
                string description = row["Description"].ToString();
                string url = row["Url"].ToString();
    
                return new MenuNode() { Id=nodeId, ParentId=parentId, Description=description, Url=url };
            }
        }
    }
    

    MenuNode.cs

    using System;
    using System.Collections.Generic;
    using System.Web.UI.WebControls;
    
    namespace MenuTreeDemo
    {
        public class MenuNode
        {
            public int Id { get; set; }
            public int ParentId { get; set; }
            public string Description { get; set; }
            public string Url { get; set; }
            public List<MenuNode> Children { get; set; }
    
    
            public MenuNode ()
            {
                Children = new List<MenuNode>();
            }
    
    
            // Will visit all descendants and turn them into menu items.
            public MenuItem ToMenuItem()
            {
                MenuItem item = new MenuItem(Description) { NavigateUrl=Url };
                foreach (MenuNode child in Children)
                {
                    item.ChildItems.Add(child.ToMenuItem());
                }
    
                return item;
            }
        }
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I am using Asp.net menu control for the web site that I am currently
I am currently using ASP.NET MVC 2.0 RC2, having recently moved from version 1.0.
I have a ASP.Net 2.0 website that is currently using a custom MembershipProvider and
I'm currently using Subversion to manage my ASP.NET website. I'm finding that whenever I
I am currently using asp.NET MVC to build a Content Management System parts of
I am currently using ASP.NET MVC1 in my project but now i am planing
Im currently using vs2008 with asp.net mvc framework for web development. Im missing a
I am currently building an application using ASP.NET MVC. The data entry pages are
I just found out about superfish and currently using it on an asp.net. The
I am currently creating an e-commerce site using C# ASP.NET MVC and have just

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.