如何在asp.net中使用菜单控件创建从数据库的动态菜单? [英] How to Create Dynamic Menu from Database using Menu control in asp.net?

查看:156
本文介绍了如何在asp.net中使用菜单控件创建从数据库的动态菜单?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想在菜单控制,以创建数据库并显示一个菜单。

I want to create a menu from Database and show in Menu Control.

code在这里,在.aspx页面中:

Code Here in .aspx page:

 <asp:Menu ID="Menu1" Orientation="horizontal" StaticMenuItemStyle-CssClass="menuItem"
                            DynamicMenuItemStyle-CssClass="menuItem" runat="server">

在师父的.cs页:

 protected void Page_Load(object sender, EventArgs e)
    {
        if (!IsPostBack)
        {

            populateMenuItem();
        }

    }

    private void populateMenuItem()
    {

        DataTable menuData = GetMenuData();
        AddTopMenuItems(menuData);

    }
    /// Filter the data to get only the rows that have a
    /// null ParentID (This will come on the top-level menu items)

    private void AddTopMenuItems(DataTable menuData)
    {
        DataView view = new DataView(menuData);
        view.RowFilter = "DepartmentParentID IS NULL";
        foreach (DataRowView row in view)
        {
            //MenuItem newMenuItem = new MenuItem(row["DepartmentName"].ToString(), row["DepartmentID"].ToString());
            MenuItem newMenuItem = new MenuItem(row["DepartmentName"].ToString(), row["DepartmentID"].ToString());

            Menu1.Items.Add(newMenuItem);
            AddChildMenuItems(menuData, newMenuItem);
        }

    }
    //This code is used to recursively add child menu items by filtering by ParentID

    private void AddChildMenuItems(DataTable menuData, MenuItem parentMenuItem)
    {
        DataView view = new DataView(menuData);
        view.RowFilter = "DepartmentParentID=" + parentMenuItem.Value;
        foreach (DataRowView row in view)
        {
            MenuItem newMenuItem = new MenuItem(row["DepartmentName"].ToString(), row["DepartmentID"].ToString());
            parentMenuItem.ChildItems.Add(newMenuItem);
            AddChildMenuItems(menuData, newMenuItem);
        }
    }


    private DataTable GetMenuData()
    {
        using (SqlConnection con = new SqlConnection(conStr))
        {

            using (SqlCommand cmd = new SqlCommand("SELECT  DepartmentID,OfficeID,DepartmentName,DepartmentParentID,IsActive,CreatedByID,CreatedDate,LastModifiedByID,LastModifiedDt FROM DepartmentMst", con))
            {
                SqlDataAdapter da = new SqlDataAdapter(cmd);
                DataTable dt = new DataTable();
                da.Fill(dt);
                return dt;
            }

        }
    }

问题是在AddTopMenuItems()方法,其中显示未将对象引用设置到对象实例在行Menu1.Items.Add(newMenuItem);
不知道为什么?

The Problem is in AddTopMenuItems() Method where it shows "Object reference not set to instance of an object" at line Menu1.Items.Add(newMenuItem); Don't know Why?

下面是数据在SQLSERVER2008 DepartmentMst:

Here is Data in SQLSERVER2008 DepartmentMst:

DepartmentID  DepartmentName IsActive   DepartmentParentID
1               HR            1            NULL
2               IT            1            NULL
3            Operations    1                NULL
4            Desktop Engineer 1             2
5           Network Engineer  1             2
6           Employee Salary   1             1

当DepartmentParentID为NULL,那么它是主菜单,如果不为null,则是子节点与尊重它的父ID。

When the DepartmentParentID is NULL then it is Main Menu and if not null then it is Child node with respected to its Parent ID.

在此示例<一个href=\"http://chandradev819.word$p$pss.com/2011/07/03/how-to-bind-asp-net-menu-control-with-database/\" rel=\"nofollow\">http://chandradev819.word$p$pss.com/2011/07/03/how-to-bind-asp-net-menu-control-with-database/

帮助鸭preciated!

Help Appreciated!

推荐答案

我怀疑您放置菜单控制的母版页的内容占位符:

I suspect that you've placed the menu control inside the content place holder of the master page:

<asp:ContentPlaceHolder ID="ContentPlaceHolder1" runat="server">
            <asp:Menu ID="Menu1" Orientation="horizontal" StaticMenuItemStyle-CssClass="menuItem"
                DynamicMenuItemStyle-CssClass="menuItem" runat="server" />
        </asp:ContentPlaceHolder>

一个ContentPlaceHolder控件用于定义可以从与母版页(显示子页面)相关联的另一页的内容替换母版页的一个区域。

A ContentPlaceHolder control is used to define a region of the Master Page which can be substituted with content from another page associated with the master page(display child pages).

由于菜单将所有的页面从母版页继承之间的共享控制简单移动菜单随时随地的之外内容占位符,你会好到哪里去:

Since the menu will be a shared control among all the pages inheriting from your master page simply move the menu anywhere outside the content place holder and you'll be good to go:

<asp:Menu ID="Menu1" Orientation="horizontal" StaticMenuItemStyle-CssClass="menuItem"
    DynamicMenuItemStyle-CssClass="menuItem" runat="server" />
<asp:ContentPlaceHolder ID="ContentPlaceHolder1" runat="server">
</asp:ContentPlaceHolder>

这篇关于如何在asp.net中使用菜单控件创建从数据库的动态菜单?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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