You must Sign In to post a response.
  • Category: .NET

    How to hide the menu based on user

    < asp:Menu id="Menu1" runat="server" width="200px" >
    < Items >
    < asp:MenuItem Text="EMPLOYEE DETAILS" NavigateUrl="~/Employee_Details.aspx"
    Value="Employee_Details" >
    < /asp:MenuItem >

    <asp:MenuItem Text="HR" NavigateUrl="~/HR.aspx" Value="HR">

    <asp:MenuItem Text="Manager" NavigateUrl="~/manager.aspx" Value="Manager">





    i want to hide the HR and Manager menu once user login show the Employee menu and remove the HR and Manager menu.

    Note once user login it will redirect to Home page. home.aspx

    in home.aspx all the menu name are visible.


    i tried the below code once user login in to home page shows the Employee menu only and remove the HR and Manager menu.


    In Login page i written the below code as follows

    if (txtuser.Text == "admin")
    {
    var menu = Page.Master.FindControl("Menu1") as Menu;
    if (menu != null)
    {
    menu.Items.Remove(menu.FindItem("Attendance"));
    menu.Items.Remove(menu.FindItem("Manager"));
    }
    }

    from the above code i am removing the Attendance and HR menu.

    but when user login the home.aspx all the menus are displaying Employee ,Attendance and Manager.

    what is the mistake in my above code.
  • #769478
    please let me know the answer for my above questions

  • #769501
    Hi Follow following steps
    <configuration>
    <system.web>
    <authentication mode="Forms"/>
    <authorization> <deny users="?"/> //this will restrict anonymous user access
    </authorization>

    </system.web>
    <location path="register.aspx"> ~/publicpages/register.aspx
    <system.web>
    <authorization>
    <allow users="*"/> // this will allow access to everyone to register.aspx
    </authorization>
    </system.web>
    </location>
    </configuration>

    Allow only users in particular Role

    Here I am will not show how to setup roles. We will see now what needs to be done in web.config to configure authorization for a particular role. e.g You have two roles. HR and Employee and two folders HR and Employee. Users in HR role can access both folders. Users in Employee role can access only EmployeeFolder and not HRFolder. You will have to add location tags for each folder path as shown below:

    <location path="HRFolder">
    <system.web>

    <authorization>
    <allow roles="HR"/> //Allows users in Admin role

    <deny users="*"/> // deny everyone else
    </authorization>

    </system.web>
    </location>

    <location path="EmpkoyeeFolder">
    <system.web>

    <authorization>
    <allow roles="HR,Employees"/> //Allow Employees in HR and Employee roles

    <deny users="*"/> // Deny rest of all
    </authorization>

    </system.web>
    </location>


    Hope this will help


  • Sign In to post your comments