2012-11-12 35 views
1

我想從數據庫創建一個菜單並在菜單控制中顯示。如何使用asp.net中的菜單控件從數據庫創建動態菜單?

代碼這裏在.aspx頁面中:

<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); 不知道爲什麼?

下面是數據在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子節點。

樣品這裏http://chandradev819.wordpress.com/2011/07/03/how-to-bind-asp-net-menu-control-with-database/

幫助感激!

回答

4

我懷疑你放在裏面菜單控制母版頁的內容佔位符:

​​

ContentPlaceHolder控件是用來定義母版頁可以取代的區域與來自與母版頁關聯的另一頁面的內容(顯示子頁面)。

由於菜單將所有頁面從母版頁繼承只需移動菜單隨時隨地內容佔位符之間的共享控制,你會好起來:

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

感謝@ Deni ...現在我知道了...... :) –

+0

Plz告訴我如何從Master頁面獲取Content Page中的Selected MenuItem。例如:如果Department是「DesktopEngineer」,那麼我需要它存儲在數據庫中的相應值? –

+0

看看這篇文章 - http://stackoverflow.com/questions/2770042/set-item-selected-in-asp-net-menu-control –