Click here to Skip to main content
15,903,385 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Hi all,
I have to use ASPxNavBar menu in my master page. how to add menus with heading and items from DB.
I am having Form name, Header, link in the DB table.
How can we do it?
Posted
Updated 12-Jan-12 22:57pm
v2

1 solution

Prasad,

Try the following, might not be most efficient but works.

Here is the ASPX
XML
<dx:ASPxRoundPanel ID="ASPxRoundPanel2" HeaderText="Some Menu" runat="server" Width="205px"
    Theme="Office2010Blue">
    <PanelCollection>
        <dx:PanelContent>

            <dx:ASPxNavBar ID="ASPxNavBar1" runat="server" Theme="Office2010Blue" Width="100%">
            </dx:ASPxNavBar>

        </dx:PanelContent>
    </PanelCollection>
</dx:ASPxRoundPanel>


Here is the c# Code...

public partial class uc_menu : System.Web.UI.UserControl
    {
        List<MenuInfo> menuItems = new List<MenuInfo>();
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                int i = 1;
                // I am populating my list with Menu Items. You could easily substitute a data from a Database
                menuItems.Add(new MenuInfo() { MenuId = i++, MenuName = "ItemName1", MenuText = "itemText1", MenuUrl = @"http://Somesite/Page1.aspx", MenuGroup = "Group1" });
                menuItems.Add(new MenuInfo() { MenuId = i++, MenuName = "ItemName2", MenuText = "itemText2", MenuUrl = @"http://Somesite/Page2.aspx", MenuGroup = "Group1" });
                menuItems.Add(new MenuInfo() { MenuId = i++, MenuName = "ItemName3", MenuText = "itemText3", MenuUrl = @"http://Somesite/Page3.aspx", MenuGroup = "Group3" });
                menuItems.Add(new MenuInfo() { MenuId = i++, MenuName = "ItemName4", MenuText = "itemText4", MenuUrl = @"http://Somesite/Page4.aspx", MenuGroup = "Group1" });
                menuItems.Add(new MenuInfo() { MenuId = i++, MenuName = "ItemName5", MenuText = "itemText5", MenuUrl = @"http://Somesite/Page5.aspx", MenuGroup = "Group2" });
            }

            // My Method to build the Menu Items. 
            // I pass in my NavBar Control, List of menuItems I created above and a Title for the Round Panel Control
            //
            BuildNavBarItems(this.ASPxNavBar1, menuItems, "Main Menu Title");

        }

        private void BuildNavBarItems(ASPxNavBar navbar, List<MenuInfo> menuitems, string menutitle)
        {

            // Set the Title
            this.ASPxRoundPanel2.HeaderText = menutitle;
            string navUrl = string.Empty;
            
            // I use a Dictionary object to keep track of the Menu Groups

            Dictionary<string, NavBarGroup> navGroups =
                new Dictionary<string, NavBarGroup>();

            // Clear the Items to start fresh.
            navbar.Groups.Clear();


            foreach (MenuInfo m in menuitems)
            {
                navUrl = m.MenuUrl;

                // If the Key Exists in the Dictionary add the new Item to the items collection of that NavGroup
                if (navGroups.ContainsKey(m.MenuGroup))
                    navGroups[m.MenuGroup].Items.Add(new NavBarItem(m.MenuText, "", "", navUrl));
                else
                {
                    // Otherwise we add a new navGroup to the Navbar collection, add the navbarItem to it and store it in the dictionary
                    NavBarGroup navgroup = navbar.Groups.Add(m.MenuGroup, m.MenuGroup);
                    navgroup.Items.Add(new NavBarItem(m.MenuText, "", "", navUrl));
                    navGroups.Add(m.MenuGroup, navgroup);
                }
            }
        }

        private class MenuInfo
        {
            public int MenuId { get; set; }
            public string MenuName { get; set; }
            public string MenuText { get; set; }
            public string MenuUrl { get; set; }
            public string MenuGroup { get; set; }
        }
    }


I hope this helps.
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900