Add items to menu dynamically in asp.net website(在 asp.net 网站中动态添加项目到菜单)
问题描述
<asp:Menu ID="mnu" runat="server" PathSeparator="," CssClass="menu" DynamicMenuItemStyle-CssClass="menu">
<Items>
<asp:MenuItem Text="home" NavigateUrl="~/Default.aspx" />
<asp:MenuItem Text="Aboutus" NavigateUrl="#"/>
<asp:MenuItem Text="Support" NavigateUrl="#" />
</Items>
</asp:Menu>
我在母版页中有这个菜单,当用户登录网站时,基于用户角色,我想从服务器端向菜单添加项目.我该怎么做.
I have this menu in master page, When the user logs into the website, based on the user role I want to add items to the menu from the server side. How can I do that.
Admin(添加菜单--> 组织、消息、组)用户(要添加的菜单 --> 消息、群组)
Admin(menu to add --> Organisation, Message, Group) Users(menu to add --> Message, Group)
因为我有 6 个角色,所以每个角色都有不同的菜单项.这怎么办
Since I have 6 roles I have different menu item for each role. How can this be done
推荐答案
在master的Page_Load
中可以查看用户是否在某些角色中,动态添加值到菜单:>
In the Page_Load
of the master you could check whether the user is in some roles and dynamically add values to the menu:
protected void Page_Load(object sender, EventArgs e)
{
if (User.IsInRole("admin"))
{
mnu.Items.Add(new MenuItem
{
Text = "Administer web site",
NavigateUrl = "~/admin.aspx"
});
}
}
这篇关于在 asp.net 网站中动态添加项目到菜单的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!
本文标题为:在 asp.net 网站中动态添加项目到菜单


- 为什么 C# 中的堆栈大小正好是 1 MB? 2022-01-01
- 使用 rss + c# 2022-01-01
- 带问号的 nvarchar 列结果 2022-01-01
- 是否可以在 .Net 3.5 中进行通用控件? 2022-01-01
- CanBeNull和ReSharper-将其用于异步任务? 2022-01-01
- Windows 喜欢在 LINUX 中使用 MONO 进行服务开发? 2022-01-01
- C# 通过连接字符串检索正确的 DbConnection 对象 2022-01-01
- 在 LINQ to SQL 中使用 contains() 2022-01-01
- 在 C# 中异步处理项目队列 2022-01-01
- Azure Active Directory 与 MVC,客户端和资源标识同一 2022-01-01