C# AspMenu help needed

Soldato
Joined
1 Feb 2006
Posts
8,188
Been struggling to get my head around this one. I have a hierarchy of Term objects that I want to iterate through and add to an asp menu control. I think my recursive function is fine except that each time I need to add the child items to the corresponding parent item, then add the full structure to a navigation control.

See code:

Code:
private void IterateSubTerms(Term t)
        {
            if (t.Terms != null)
            {
                foreach (Term term in t.Terms)
                {
                    MenuItem childItem = new MenuItem();
                    childItem.Text = term.Name.ToString();
                    childItem.NavigateUrl = "default.aspx";
                    
                    // next line is the problem
                    myMenu.Items.Add(childItem)
                    IterateSubTerms(term);
                }
            }
        }

The line myMenu.Items.Add is the problem as this will result in only a 2-level navigation structure. Instead what I need is to be able to add child nodes to the corresponding parent node so that I have maybe 4 or 5 levels deep.

What am I missing here? I could maybe use something like myMenu.Items[0].ChildItems.Add but I have no idea where to get the index value from that would drive this.

Any help would be great.
 
Associate
Joined
25 Jun 2006
Posts
252
I'd split off getting the root items and put a recursive function on adding the children, please excuse any mistakes I've put in this thrown together example but I'm just on my way out the door, pub :)

Code:
    public class MenuDataItem
    {
        public int Id { get; set; }
        public string Title { get; set; }
        public string Description { get; set; }
    }

    public class Class1
    {
        private readonly List<MenuDataItem> _rootCollection = new List<MenuDataItem>();

        private void InitiatingMethodOrEvent()
        {
            var menu = new Menu();
            var root = new MenuItem();

            foreach (var rootItem in _rootCollection)
            {
                var rootMenuItem = BuildMenuItem(rootItem);
                AddChildNodes(rootMenuItem, rootItem);
                root.ChildItems.Add(rootMenuItem);
            }

            menu.Items.Add(root);
        }

        private void AddChildNodes(MenuItem menuItem, MenuDataItem dataItem)
        {
            //add child nodes recursive

            foreach (var child in GetChildItems(dataItem.Id))
            {
                var node = BuildMenuItem(child);
                menuItem.ChildItems.Add(node);
                AddChildNodes(node, child);
            }
        }

        private IEnumerable<MenuDataItem> GetChildItems(int parentId)
        {
            //get child items by parentId
        }

        private static MenuItem BuildMenuItem(MenuDataItem rootItem)
        {
            //set properties of menu item object below

            var returnItem = new MenuItem();
            returnItem.NavigateUrl = rootItem.Id.ToString();
            returnItem.Text = rootItem.Title;
            returnItem.ToolTip = rootItem.Description;

            return returnItem;
        }
    }

Hope this helps and isn't complete jibberish lol...

J.
 
Back
Top Bottom