Go To Index Page

Populating Menu using Database

WAY 1.

Create following tables in Database:

Menu

MenuID MenuText ParentMenuID MenuURL Description Status
1 Home 0 ContentPage1.aspx Home Page 1
2 Masters 0 # Masters 1
3 Tools 0 # Tools 1
4 Reports 0 # Reports 1
5 Manage Companies 2 # Companies 1
6 Parent Companies 5 ContentPage1.aspx Parent Companies 1
7 Sub Companies 5 ContentPage1.aspx Sub Companies 1
8 Users 2 # Users 1
9 Promotions 2 ContentPage1.aspx Promotions 1
10 Mobile Change 3 # Mobile Change 1
11 Transactions 4 # Transactions 1
12 Allocations 4 # Allocations 1
13 Create User 8 # Create User 1
14 View User 8 # View User 1
15 Edit User 14 # Edit User 1

Role

RoleID UserDescription UserRoleKeyWord AccessLevel
1 SUPER ADMINISTRATOR PSA 0
2 ACCOUNT MANAGER PAM 1
3 CUSTOMER SUPPORT MANAGER PCM 4
4 RISK MANAGER PRM 5
5 CUSTOMER SUPPORT EXECUTIVE PCE 6
6 RISK EXECUTIVE PRE 7
7 COMPANY ADMINISTRATOR CA 2
8 COMPANY ACCOUNT MANAGER CAM 3
9 OPERATION TEAM POT 100
10 ALLIENCE TEAM PAT 100

MenuRoleMatrix

MatrixID MenuID RoleID Status
1 1 1 1
2 2 1 1
3 3 1 1
4 4 1 1
5 5 1 1
6 6 1 1
7 7 1 1
8 8 1 1
9 9 1 1
10 10 1 1
11 11 1 1
12 12 1 1
13 13 1 1
14 14 1 1
15 15 1 1

using System;
using System.Data;
using System.Configuration;
using System.Collections;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;

public partial class HMasterPage : System.Web.UI.MasterPage
{
protected void Page_Load(object sender, EventArgs e)
{
if(!Page.IsPostBack)
{
FillMenu();
}
}

private void FillMenu()
{
clsUtilDA clsUDA = new clsUtilDA();
DataSet ds = new DataSet();
ds = clsUDA.GetDataSet("SELECT MENUID, MENUTEXT, PARENTMENUID, MENUURL FROM MENU WHERE STATUS = 1 AND MENUID IN (SELECT MENUID FROM MenuRoleMatrix WHERE STATUS = 1 AND ROLEID =1) ORDER BY PARENTMENUID");

if(ds != null && ds.Tables[0] != null && ds.Tables[0].Rows.Count > 0 )
{
foreach(DataRow row in ds.Tables[0].Rows)
{
if(row["PARENTMENUID"].ToString() == "0")
MenuMaster.Items.Add(new MenuItem(row["MENUTEXT"].ToString(),row["MENUID"].ToString(),"",row["MENUURL"].ToString(),"_self"));
else
{
if(MenuMaster.FindItem(row["PARENTMENUID"].ToString()) != null)
MenuMaster.FindItem(row["PARENTMENUID"].ToString()).ChildItems.Add(new MenuItem(row["MENUTEXT"].ToString(),row["MENUID"].ToString(),"",row["MENUURL"].ToString(),"_self"));
else
{
foreach(MenuItem subMenu in MenuMaster.Items)
{
DigMenu(subMenu, row["PARENTMENUID"].ToString(),row["MENUTEXT"].ToString(),row["MENUID"].ToString(),row["MENUURL"].ToString());
}
}
}
}
}
}

private void DigMenu(MenuItem subMenu,string parentID,string text,string mid,string mUrl)
{
if(subMenu.ChildItems.Count > 0)
{
foreach(MenuItem itm in subMenu.ChildItems)
{
if(itm.Value == parentID)
{
itm.ChildItems.Add(new MenuItem(text,mid,"",mUrl,"_self"));
break;
}
else
DigMenu(itm,parentID,text,mid,mUrl);
}
}
}
}

WAY 2.

Suppose that you have some data in the database tables using which you want to populate the Menu. Although I prefer the .sitemap technique since that is its sole purpose, the code below shows how you can use database as a data source.

private void PopulateMenu()
{
DataSet ds = GetDataSetForMenu();
Menu menu = new Menu();

foreach (DataRow parentItem in ds.Tables["Categories"].Rows)
{
MenuItem categoryItem = new MenuItem((string)parentItem["CategoryName"]);
menu.Items.Add(categoryItem);

foreach (DataRow childItem in parentItem.GetChildRows("Children"))
{
MenuItem childrenItem = new MenuItem((string)childItem["ProductName"]);
categoryItem.ChildItems.Add(childrenItem);
}
}

Panel1.Controls.Add(menu);
Panel1.DataBind();
}

private DataSet GetDataSetForMenu()
{
SqlConnection myConnection = new SqlConnection(GetConnectionString());
SqlDataAdapter adCat = new SqlDataAdapter("SELECT * FROM Categories", myConnection);
SqlDataAdapter adProd = new SqlDataAdapter("SELECT * FROM Products", myConnection);

DataSet ds = new DataSet();
adCat.Fill(ds, "Categories");
adProd.Fill(ds, "Products");
ds.Relations.Add("Children",
ds.Tables["Categories"].Columns["CategoryID"],
ds.Tables["Products"].Columns["CategoryID"]);
return ds;
}
Let's first look at the GetDataSetForMenu() since its being called from PopulateMenu(). In the GetDataSetForMenu() method we make a relationship between the Categories table and the Products table.

The PopulateMenu() methods receives the DataSet from the GetDataSetForMenu() and iterates through the DataSet. For each iteration it creates a new MenuItem and adds it into the menu items collection.

-------------------

Populating the Menu using SiteMapDataSource
This is the preferred way of populating the Menu control. SiteMapDataSource allows you to read the information from the .sitemap file which is simply an XML file. Let's take a look at the .sitemap file. As you can see in the file below, all you need to do is to assign the values to the nodes and that's it.

Now let's see how we build our menu using the .sitemap file above. All we are doing in the GetSiteMapDataSource method is getting the information from the Web.sitemap file and building the menu based on the contents of that file.

private void CreateMenuControl()
{
Menu1.DataSource = GetSiteMapDataSource();
Menu1.DataBind();
}

private SiteMapDataSource GetSiteMapDataSource()
{
XmlSiteMapProvider xmlSiteMap = new XmlSiteMapProvider();
System.Collections.Specialized.NameValueCollection
myCollection = new
System.Collections.Specialized.NameValueCollection(1);
myCollection.Add("siteMapFile", "Web.sitemap");
xmlSiteMap.Initialize("provider", myCollection);
xmlSiteMap.BuildSiteMap();
SiteMapDataSource siteMap = new SiteMapDataSource();
return siteMap;
}

-----------------------------

Populating the Menu Control Using an XML File
private void CreateMenuWithXmlFile()
{
string path = @"C:\MyXmlFile.xml";
DataSet ds = new DataSet();
ds.ReadXml(path);
Menu menu = new Menu();
menu.MenuItemClick += new MenuEventHandler(menu_MenuItemClick);

for (int i = 0; i < ds.Tables.Count; i++)
{
MenuItem parentItem = new MenuItem((string)ds.Tables[i].TableName);
menu.Items.Add(parentItem);

for (int c = 0; c < ds.Tables[i].Columns.Count; c++)
{
MenuItem column = new MenuItem((string)ds.Tables[i].Columns[c].ColumnName);
menu.Items.Add(column);

for (int r = 0; r < ds.Tables[i].Rows.Count; r++)
{
MenuItem row = new MenuItem((string)ds.Tables[i].Rows[r][c].ToString());
parentItem.ChildItems.Add(row);
}
}
}

Panel1.Controls.Add(menu);
Panel1.DataBind();
}
I get the XML from the DataSet ReadXML() method. And after that it's just a matter of iterating through the DataSet and making MenuItems.

Go To Index Page