0

I am a new ASP.NET Web Forms developer and I am struggling now in hiding and showing ASP.NET Menu Control Items based on the user role. The roles are defined by me in the database and based on the result of checking the role of the user, the system should show or hide some of the menu items.

I have the following ASP.NET Menu Control in the Master Page:

<asp:Menu ID="Menu" runat="server" PathSeparator="," Orientation="Horizontal" EnableViewState="false">
    <Items>
        <asp:MenuItem Text="Home" NavigateUrl="~/Pages/Default.aspx" Value="home"></asp:MenuItem>
        <asp:MenuItem Text="Sheet" NavigateUrl="~/Pages/Sheet.aspx" Value="sheet"></asp:MenuItem>
        <asp:MenuItem Text="Test" NavigateUrl="~/Pages/Test.aspx" Value="test"></asp:MenuItem>
    </Items>
</asp:Menu>

And in the code-behind of the master page I am doing the following logic:

protected void MenuAccess()
 {
    if(Acccess.HasAccess(username))
    {
        if(Access.IsAdmin(username))
        {
            SetMenuItemUrl("sheet", "~/Pages/Sheet.aspx?UserId=");
            HideMenuItem("test");
        }
        if(Access.IsSupport(username))
        {
            SetMenuItem("test");
        }
    }
 }

 protected void HideMenuItem(string valuePath)
    {
        SetMenuItem(valuePath, false, null);
    }

    protected void SetMenuItemUrl(string valuePath, string url)
    {
        SetMenuItem(valuePath, true, url);
    }

    protected void SetMenuItem(string valuePath, bool visible, string url)
    {
        var item = Menu.FindItem(valuePath);
        if (item != null)
        {
            if (url != null)
                item.NavigateUrl = url;

            if (visible == false)
            {
                if (valuePath.LastIndexOf(',') < 0)
                    Menu.Items.Remove(item);
                else
                {
                    MenuItem parent = Menu.FindItem(valuePath.Substring(0, valuePath.LastIndexOf(',')));
                    parent.ChildItems.Remove(item);
                }
            }
        }
    }

However, if the user has two roles: admin and support, then the menu item with 'test' value will not be displayed and I don't know why. Could you please help me with this?

Technology Lover
  • 259
  • 1
  • 7
  • 19

1 Answers1

0

Your code is removing the item from the menu but when you go to set it later on, you are never adding it back into the menu items.

Perhaps instead of removing the menu item, can you change it's visibility instead? This would allow you to hide / show it depending on the visible bool. You'll also need to implement that in your SetMenuItem function.

Here is the MSDN documentation for the MenuItem class. I don't see a visibility setting but you might be able to use Enabled or Selected depending on your needs. This will however show the option for everybody, they just won't be able to interact with it. Not sure if that's acceptable or not.

Something like this should get you close. You'll have to test to see how it works.

protected void SetMenuItem(string valuePath, bool visible, string url)
{
    var item = Menu.FindItem(valuePath);
    if (item != null)
    {
        if (url != null)
            item.NavigateUrl = url;

        item.Enabled = visible;
    }
}
Brian Dishaw
  • 5,767
  • 34
  • 49
  • thanks for your help. Could you please help me by modifying the method I have in my code? I am struggling with modifying it. Thanks in advance. – Technology Lover Mar 28 '15 at 17:42