Hi
While working on asp.net project, we will get situation to create a dynamic menu control which will be configurable by admin.
For doing this task we can do like this
Step1: I have created a table like this Img
Step2: Configure the asp.net menu control like this
<%@ Page Language="C#" AutoEventWireup="true" CodeFile="Default.aspx.cs" Inherits="_Default" %>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
<title></title><style type="text/css">
.menuItem
{
border:Solid 1px black;
width:100px;
padding:2px;
background-color:#eeeeee;
}
.menuItem a
{
color:blue;
}.IE8Fix
{
z-index: 1000;}
</style>
</head>
<body>
<form id="form1" runat="server">
<div>
<asp:Menu
id="Menu1"
Orientation="horizontal"
StaticMenuItemStyle-CssClass="menuItem"
DynamicMenuItemStyle-CssClass="menuItem"
Runat="server">
<%– This CSS used for fixing the browser problem with IE8–%>
<DynamicMenuStyle CssClass="IE8Fix" /></asp:Menu>
</div>
</form>
</body>
</html>
Step3: Write the code for binding asp.net menu on basis of category like this
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Data.SqlClient;
using System.Data;public partial class _Default : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{if (!IsPostBack)
{populateMenuItem();
}
}private void populateMenuItem()
{DataTable menuData = GetMenuData();
AddTopMenuItems(menuData);}
private DataTable GetMenuData()
{
using (SqlConnection con = new SqlConnection("Data Source=.\\SQLEXPRESS;AttachDbFilename=|DataDirectory|\\Database.mdf;Integrated Security=True;User Instance=True"))
{
using (SqlCommand cmd = new SqlCommand("SELECT Id,ParentId,Name FROM tblProduct", con))
{
SqlDataAdapter da = new SqlDataAdapter(cmd);
DataTable dt = new DataTable();
da.Fill(dt);
return dt;
}}
}/// 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 = "ParentID IS NULL";
foreach (DataRowView row in view)
{
MenuItem newMenuItem = new MenuItem(row["Name"].ToString(), row["Id"].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 = "ParentID=" + parentMenuItem.Value;
foreach (DataRowView row in view)
{
MenuItem newMenuItem = new MenuItem(row["Name"].ToString(), row["Id"].ToString());
parentMenuItem.ChildItems.Add(newMenuItem);
AddChildMenuItems(menuData, newMenuItem);
}
}}
You can download the source code from here
Dynamic Menu
i have 2 similar post regarding hierarchical data display:
http://www.codeshode.com/2011/07/display-hierarchical-data-with-menu.html
http://www.codeshode.com/2010/05/display-hierarchical-data-with-treeview.html
http://www.codeshode.com/2010/06/display-directory-structure-using.html
Hi
I saw your post. It is really nice and useful. Thanks for sharing the nice URL.
Great And Nice… Thanks A LOT….!!!!
You are welcome.
Its really nice post, Could you please tel me code to navigate to another pages (I mean if we click on India (child node), redirects to India.aspx page)
Hi
I glad to know that you liked it. That is also possible like this
protected void Menu1_MenuItemClick(object sender, MenuEventArgs e)
{
string name = Menu1.SelectedItem.Text;
string pageurl=name+”.aspx”;
Response.Redirect(pageurl);
}
Regards
Chandradev
Thanks Alot
Post to how we navigate to another Page
Sorry for the late reply. You click on “MenuItemClick” event and write this code. Then it will go to particular page
protected void Menu1_MenuItemClick(object sender, MenuEventArgs e)
{
string name = Menu1.SelectedItem.Text;
string pageurl=name+”.aspx”;
Response.Redirect(pageurl);
}
I am not able to get click event of second menu item & its sub items..
MenuItemClick event is working fine for first menu & its sub menus but not working for the rest..
Please give me some time to check the code.
hi sir , i have completed all the navigation part also but only problem is firstly menuitem.click is not happened and another thing is when menu item is click the content of the lastmenu is displaying for this am sending all my code to plz solve my problem that to in dynamically
Thanks a lot.really very useful
My menu is over loaded by the page. How to get the menu on the top of the page?
Hi, could you explain your problem little bit more ? I didnot get you. You means your sub menu is so lenghly . do You want to see it on top ?
thks for the help
Ther eis no error in yor code but i am not getting child node , i can view Menus only
for example, Country Name and State name
iits coming But i amnot getting that india and Nepal ( used Your same Code)
and More thing i want to add image near menus…
There is no error in your code. but i am not getting child menus, i can view Menus only
for example, Country Name and State name is coming But i am not getting that india and Nepal ( used Your same Code)
and one More thing i want to add image near menus…
HI
Sorry for late reply. I m not getting any problem. I have uploaded the code. Please check it.
Hello sir…
Your code is really very helpfull…
Jst do the more needful…
I have created the menu ,submenu…The only problem is i m unable to do navigation on the click of any sub menu or sub submenu items…
I also wanted that if user click on any of the menu….I should get the ID of that particular menu and sub menu..
Pls help me ASAP..
HERE IS MY CODE
private void populateMenuItem()
{
DataTable menuData = GetMenuData();
AddTopMenuItems(menuData);
}
private DataTable GetMenuData()
{
System.Data.OleDb.OleDbConnection conn = new OleDbConnection(strConn);
string strSqlQuery = “SELECT MenuID,MenuName,Handler,ParentID FROM Mainmenu”;
System.Data.OleDb.OleDbCommand cmd = new OleDbCommand(strSqlQuery, conn);
System.Data.OleDb.OleDbDataAdapter objDA = new OleDbDataAdapter(cmd);
DataTable dt = new DataTable();
objDA.Fill(dt);
return dt;
}
/// 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 = “ParentID = 0”;
foreach (DataRowView row in view)
{
MenuItem newMenuItem = new MenuItem(row[“MenuName”].ToString(), row[“MenuID”].ToString(), row[“Handler”].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 = “ParentID=” + parentMenuItem.Value;
foreach (DataRowView row in view)
{
MenuItem newMenuItem = new MenuItem(row[“MenuName”].ToString(), row[“MenuID”].ToString());
parentMenuItem.ChildItems.Add(newMenuItem);
AddChildMenuItems(menuData, newMenuItem);
for (int i = 0; i < view.Count; i++)
{
int iRowCount = view.ToTable(true, "MenuID").Rows.Count;
}
}
}
I occasionally publish commentary, nonetheless I looked through a
huge amount of responses here on How to bind asp.
net menu control with database ? | Chandradev’s Blog but had a couple of thoughts for you if you wouldn’t
mind. Could it be just me or do quite a few of the above remarks seem to be as if they are provided
by really dumb persons? Also, when you’re posting on other various internet pages, I’d
like to stay in touch with you. Might you post a listing of
all of your social network sites such as your linkedin user profile, Facebook page
or twitter feed?
Hi there, I found your site by the use of Google while looking
for a similar subject, your website came up, it appears good.
I have bookmarked it in my google bookmarks.
Hi there, simply changed into aware of your blog thru Google, and located that it is really informative.
I am gonna watch out for brussels. I’ll appreciate in the event you proceed this
in future. Numerous other folks will likely be benefited out of your writing.
Cheers!
Nice post )