c# - How to fix this issue with ASP.NET Menu Control? -


i new asp.net web forms developer , struggling in hiding , showing asp.net menu control items based on user role. roles defined me in database , based on result of checking role of user, system should show or hide of menu items.

i have following asp.net menu control in 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 code-behind of master page doing 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 user has 2 roles: admin , support, menu item 'test' value not displayed , don't know why. could please me this?

your code removing item menu when go set later on, never adding menu items.

perhaps instead of removing menu item, can change it's visibility instead? allow hide / show depending on visible bool. you'll need implement in setmenuitem function.

here msdn documentation menuitem class. don't see visibility setting might able use enabled or selected depending on needs. show option everybody, won't able interact it. not sure if that's acceptable or not.

something should close. you'll have test see how 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;     } } 

Comments

Popular posts from this blog

google chrome - Developer tools - How to inspect the elements which are added momentarily (by JQuery)? -

angularjs - Showing an empty as first option in select tag -

php - Cloud9 cloud IDE and CakePHP -