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

Time:01-27

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

   <li  id="Admin" > <a  data-toggle="dropdown"  href="#">Admin<span  ></span></a>
                                <ul  >
                                <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?

CodePudding user response:

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  id="Admin" runat="server">
  .etc. etc.

</li>
  • Related