Click here to Skip to main content
15,944,136 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
I have an menu control in master page and generating the menu content from database according the role..but now i am stuck don't know how to assign an navigation url of content page .....please tell me how do i generate an navigate url property of menu control dynamically according to the content page...
Posted

1 solution

try the below code its working on my projects. Put the below code in your master page or where ever you have the menu control placed. I have used List here. You can use data table also using the same code. PopulateChildMenuItems function is called for recursively adding child menu items

protected void Page_Load(object sender, EventArgs e)
{
if (HttpContext.Current.Session["UserDetails"] == null)
{
Response.Redirect("~/Login.aspx");
}
if (!this.IsPostBack)
{
populateMenuItems();
}
}


protected void populateMenuItems()
{
userDetails = (UserBAL)(HttpContext.Current.Session["UserDetails"]);

List<menubal> menuItems = CacheMaster.getMenuList(userDetails.UserID); //MenuDAL.getMenuList(userDetails.UserID);

List<menubal> parentMenuItems = menuItems.FindAll(x => x.ParentMenuID == 0);


foreach (MenuBAL item in parentMenuItems)
{
MenuItem mnuParent = new MenuItem();
//mnuParent.Target = item.MenuURL;
mnuParent.Text = item.MenuName;
mnuParent.NavigateUrl = item.MenuURL;
mnuParent.Value = item.MenuID.ToString();

populateChildItems(ref mnuParent, menuItems);

mnuMain.Items.Add(mnuParent);
}
}

protected void populateChildItems(ref MenuItem parentMenuItem, List<menubal> menuList)
{
Int32 parentMenuID = Convert.ToInt32(parentMenuItem.Value);
List<menubal> childItems = menuList.FindAll(x => x.ParentMenuID == parentMenuID);

foreach (MenuBAL item in childItems)
{
MenuItem childMenu = new MenuItem();

childMenu.Text = item.MenuName;
//childMenu.Target = item.MenuURL;
childMenu.NavigateUrl = item.MenuURL;
childMenu.Value = item.MenuID.ToString();

populateChildItems(ref childMenu, menuList);

parentMenuItem.ChildItems.Add(childMenu);
}
}
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900