0

I have 2 different roles (administrator and regular user); based on the user that was entered only those menus they'll have access to. However, I'm not sure how to get the menu from such. Their roles are stored in a database table. This is what I have so far in the design window.

<asp:Menu ID="Menu1" runat="server" DynamicHoverStyle-BackColor="#99ccff" Orientation="Horizontal" Font-Size="X-Large" ForeColor="#003366" DynamicEnableDefaultPopOutImage="False" ScrollDownImageUrl="~/Img/1.jpg" StaticEnableDefaultPopOutImage="False" >
    <DynamicHoverStyle BackColor="#99CCFF" />
    <Items>
        <asp:MenuItem NavigateUrl="~/Home/Welcome.aspx" Text="Home" Value="Home" ToolTip="Home" ></asp:MenuItem>
        <asp:MenuItem  Text="Search User"  ToolTip="Search"></asp:MenuItem>

        <asp:MenuItem Text="Add User" Value="Add User">

        <asp:MenuItem  NavigateUrl="~/Account/login.aspx" Text="Log Out"  ToolTip="Log Out"></asp:MenuItem> 
    </Items>
</asp:Menu>

Update

if (dr.Read())
{
    if (Convert.ToString(dr["RoleName"]) == "Administrator")
    {
        Menu1.Items.Add(new MenuItem
        {
            NavigateUrl = "~/Home/Welcome.aspx",
            Text = "Home",
        });
    }
}
2

2 Answers 2

1
  1. Add IDs to your menu items giving them distinct names. Set visible='false' on the admin items
  2. In your codebehind file check if the user is an admin. If so, set visible=true on the admin items.

Depending on your requirement, you could disable them (in which case they'd appear in the menu but not work unless the user was an admin).

Menu item: <asp:MenuItem ID="menu1" visible="false" Text="Add User" Value="Add User">

Codebehind would be along the lines of:

if (user.isAdmin) { menu1.Visible = true }

Sign up to request clarification or add additional context in comments.

Comments

0

You can use Session. Check if he is admin then show his menu and if he is regular user then show his menu.

if(Session["type"]=="admin")
{
//
}
else if(Session["type"]=="regularUser")
{
//
}

7 Comments

would have to pull the type from the database
Yes, you need to if so
so how the menu comes in play after the session
According to your expectation, You just need to understand if else structure. What you need to show or not that's totally up to.
do understand that structure but is when it comes to the navigation section.
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.