ASP.net How to hide a menu item from visitor? -


i want hide "admin panel" menu item visitor, without going role approach.

<?xml version="1.0" encoding="utf-8" ?> <sitemap xmlns="http://schemas.microsoft.com/aspnet/sitemap-file-1.0" >     <sitemapnode url="" title="nav"  description="">         <sitemapnode url="~/default.aspx" title="home"  description=""></sitemapnode>         <sitemapnode url="~/about.aspx" title="about"  description=""></sitemapnode>         <sitemapnode url="" title="admin panel"  description="">           <sitemapnode url="~/admin/addposts.aspx" title="add posts"  description=""></sitemapnode>           <sitemapnode url="~/admin/editposts.aspx" title="edit posts"  description=""></sitemapnode>           <sitemapnode url="~/admin/approvecomments.aspx" title="approve comments"  description=""></sitemapnode>         </sitemapnode>     </sitemapnode> </sitemap> 

master page

protected void menu1_menuitemdatabound(object sender, menueventargs e) {     if (!httpcontext.current.user.identity.isauthenticated)     {         system.web.ui.webcontrols.menu menu = (system.web.ui.webcontrols.menu)sender;         sitemapnode mapnode = (sitemapnode)e.item.dataitem;          system.web.ui.webcontrols.menuitem itemtoremove = menu.finditem(mapnode.title);          if (mapnode.title == "admin panel")         {             system.web.ui.webcontrols.menuitem parent = e.item.parent;             if (parent != null)             {                 parent.childitems.remove(e.item);             }         }     } } 

markup

    <asp:menu id="menu1" runat="server" datasourceid="sitemapdatasource1"          orientation="horizontal" onmenuitemdatabound="menu1_menuitemdatabound">     </asp:menu>     <asp:sitemapdatasource id="sitemapdatasource1" runat="server"          showstartingnode="false" /> 

i tired code above, not working. looks like, parent "admin panel" null. don't know how modify make work.

i tried version:

menu1.items.removeat(2); 

not sure why gave me index out of bound error. admin panel item 3rd item in menu1 though.

any appreciated.

could 2 ways:

markup:

<asp:menu id="navigationmenu" runat="server" cssclass="menu"                  enableviewstate="false" includestyleblock="false" orientation="horizontal"                  datasourceid="sitemapdatasource1"                  onmenuitemdatabound="navigationmenu_menuitemdatabound"> </asp:menu> <asp:sitemapdatasource id="sitemapdatasource1" runat="server" showstartingnode="false" /> 

code:

protected void navigationmenu_menuitemdatabound(object sender, menueventargs e) {     system.web.ui.webcontrols.menu menu = (system.web.ui.webcontrols.menu)sender;     sitemapnode mapnode = (sitemapnode)e.item.dataitem;      if (mapnode.title == "admin panel")     {                     system.web.ui.webcontrols.menuitem itemtoremove = menu.finditem(mapnode.title);         menu.items.remove(itemtoremove);     } } 

markup:

<asp:menu id="navigationmenu" runat="server" cssclass="menu"                  enableviewstate="false" includestyleblock="false" orientation="horizontal"                  datasourceid="sitemapdatasource1" ondatabound="navigationmenu_databound" > </asp:menu> <asp:sitemapdatasource id="sitemapdatasource1" runat="server" showstartingnode="false" /> 

code:

protected void navigationmenu_databound(object sender, eventargs e) {     ((system.web.ui.webcontrols.menu)sender).items.removeat(2); } 

Comments

Popular posts from this blog

c# - SharpSVN - How to get the previous revision? -

c++ - Is it possible to compile a VST on linux? -

url - Querystring manipulation of email Address in PHP -