如何从DB绑定ASPxNavBar [英] How to bind ASPxNavBar from DB

查看:121
本文介绍了如何从DB绑定ASPxNavBar的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

大家好,

我必须在我的母版页中使用ASPxNavBar菜单。如何使用DB中的标题和项目添加菜单。

我在数据库表格中有表格名称,标题,链接。

我们怎么做?

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?

推荐答案

Prasad,



尝试以下方法,可能效率最高但效果不错。



这是ASPX

Prasad,

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

Here is the ASPX
<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>





这是c#代码......





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.


这篇关于如何从DB绑定ASPxNavBar的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆