Search code examples
c#asp.netdrop-down-menu

ASP.NET Hide/disable entire dropdown menu based on user role


I have a drop-down list in an ASP.NET webform application generated in the sitemaster menu using the follow ing code:

   <li class="dropdown" id="Admin" > <a class="dropdown-toggle" data-toggle="dropdown"  href="#">Admin<span class="caret" ></span></a>
                                <ul class="dropdown-menu" >
                                <li><a  runat="server" href="~/Admin/Members">Members Management</a></li>
                               <li ><a  runat="server" href="~/Admin/MembersRegistry">Members Registry</a></li>

                                </ul>
                                </li>

and the script to generate the drop down is

  <script>
$(document).ready(function () {
    $(".dropdown").hover(function () {
        //toggle the open class (on/off)
        $(this).toggleClass("open");
    });
})
</script>

I want to be able to hide or show the entire dropdown menu based on user roles:

 protected void Page_Load(object sender, EventArgs e)
    {
        Admin.Visible = false;
       
        

        if (System.Web.HttpContext.Current.User.Identity.IsAuthenticated)
        {
            
            
            var manager = Context.GetOwinContext().GetUserManager<ApplicationUserManager>();
            var signinManager = Context.GetOwinContext().GetUserManager<ApplicationSignInManager>(); 
           
            var user = HttpContext.Current.User.Identity.GetUserId();

            if (manager.IsInRole(user, "Admin"))
            {
                Admin.Visible = true;
              
            }

           
        }
    }

But this is not working and the Admin control is not accessible from the code behind, any thoughts or suggestions please?


Solution

  • If you add a runat="server" tag to that, then your code behind should see/have use of that controls, and you code looks like it should work ok.

    eg:

    <li class="dropdown" id="Admin" runat="server">
      .etc. etc.
    
    </li>