Esempio n. 1
0
        private void PopulateControls()
        {
            //using (CategoryManager manager = new CategoryManager())
            //{
            List <Category> cataLevel1 = manager.Find(c => c.IsActive == true).Result.ToList();
            MenuNode        root       = ConvertToTree(cataLevel1);

            MyMenu.Items.Clear();
            foreach (MenuNode topLevelNode in root.Children)
            {
                MyMenu.Items.Add(topLevelNode.ToMenuItem());     // Visits all nodes in the tree.
            }
            //}
        }
Esempio n. 2
0
        // Assuming table is ordered.
        static MenuNode ConvertToTree(List <Category> list)
        {
            var map = new Dictionary <int?, MenuNode>();

            map[0] = new MenuNode()
            {
                Id = 0
            };                                 // root node

            MenuNode newNodeHome = new MenuNode()
            {
                Id          = int.MaxValue,
                ParentId    = 0,
                Url         = Link.BuildAbsolute("Default.aspx"),
                Description = "Home"
            };

            map[0].Children.Add(newNodeHome);

            foreach (Category item in list)
            {
                MenuNode newNode = new MenuNode()
                {
                    Id          = item.CategoryID,
                    ParentId    = 0,
                    Url         = Link.ToCategory(item.CategoryID.ToString()),
                    Description = item.Name
                };
                if (item.ParentID == null || (item.ParentID != null && manager.Find(x => x.CategoryID == item.ParentID && x.IsActive == false).Result.Count > 0))
                {
                    map[0].Children.Add(newNode);
                }
                else
                {
                    newNode.ParentId = item.ParentID;
                    map[newNode.ParentId].Children.Add(newNode);
                }
                map[item.CategoryID] = newNode;
            }
            return(map[0]); // root node
        }