I am trying to hide an Item from the Menu Items if a user is not in the Admin role. Here is the code that i am using in the code behind in my site master but the code is not hiding the Admin page in which i am trying to hide it. Here is the HTML markup for the master page:
<asp:Menu ID="NavigationMenu" runat="server" CssClass="menu" EnableViewState="false"
IncludeStyleBlock="false" Orientation="Horizontal">
<Items>
<asp:MenuItem NavigateUrl="~/Admin/Authors.aspx" Text="Admin" />
<asp:MenuItem NavigateUrl="~/Users/KCA_Summary.aspx" Text="KCA Summary" />
<asp:MenuItem NavigateUrl="~/Users/Details.aspx" Text="KCA Details" />
</Items>
</asp:Menu>
and here is the code behind:
Protected Sub Page_Load(sender As Object, e As EventArgs)
If Not Roles.IsUserInRole("Admin") Then
Dim menuItems As MenuItemCollection = NavigationMenu.Items
Dim adminItem As New MenuItem()
For Each menuItem As MenuItem In menuItems
If menuItem.Text = "Admin" Then
adminItem = menuItem
End If
Next
menuItems.Remove(adminItem)
End If
End Sub
Probably the easiest way is to use a SiteMapProvider configured with
SecurityTrimmingEnabled= true.The linked MSDN article references a lot of “How To” articles that will help you get started.