如何动态分配菜单控件的导航url属性 [英] How to assign navigate url property dynamically of menu control

查看:96
本文介绍了如何动态分配菜单控件的导航url属性的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在母版页中有一个菜单控件,并根据角色从数据库生成菜单内容。但是现在我卡住了不知道如何分配内容页面的导航网址.....请告诉我如何根据内容页面动态生成菜单控件的导航URL属性...

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...

推荐答案

尝试以下代码处理我的项目。将以下代码放在您的母版页中或放置菜单控件的位置。我在这里使用了List。您也可以使用相同的代码来使用数据表。 PopulateChildMenuItems 函数用于递归添加子菜单项



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(parentMenuItems中的MenuBAL项目)

{

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(childItems中的MenuBAL项目)

{

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);

}

}
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);
}
}


这篇关于如何动态分配菜单控件的导航url属性的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆