How to Create Dynamic Menu from Database using Menu control in asp.net?
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>
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>
SHEKHAR SHETE
Master Of Computer Application[MCA] Microsoft Certified Professional[MCP] Microsoft Certified Technology Specialist[MCTS]
Updated on June 04, 2022Comments
-
SHEKHAR SHETE over 1 year
I want to create a menu from Database and show in Menu Control.
Code Here in .aspx page:
<asp:Menu ID="Menu1" Orientation="horizontal" StaticMenuItemStyle-CssClass="menuItem" DynamicMenuItemStyle-CssClass="menuItem" runat="server">
In .cs Page of Master:
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; } } }
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?
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
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.
Sample here http://chandradev819.wordpress.com/2011/07/03/how-to-bind-asp-net-menu-control-with-database/
Help Appreciated!