开发者

How to disable a menu item on page load in asp.net 4.0

开发者 https://www.devze.com 2023-04-11 05:11 出处:网络
<Items> <asp:MenuIt开发者_高级运维em NavigateUrl=\"~/Default.aspx\" Text=\"Home\"/>
<Items>
    <asp:MenuIt开发者_高级运维em NavigateUrl="~/Default.aspx" Text="Home"/>
    <asp:MenuItem NavigateUrl="~/About.aspx" Text="About"/>
</Items>

I have this menu How can I disable it during page load

page load
If (Userrole==Something)
{
hide menu item 2
}

How can I do that.


 //Going through first level items
 if (e.item.NavigateUrl == "")
    e.item.Enabled = false;

 //Going through submenu item
foreach (MenuItem item in e.Item.Items)
{
     if (item.NavigateUrl == "")
         item.Enabled = false;
}


Add the runat and id tags to your menu items

<Items>
    <asp:MenuItem NavigateUrl="~/Default.aspx" Text="Home" id="mnuDefault" runat="server"/>
    <asp:MenuItem NavigateUrl="~/About.aspx" Text="About" id="mnuAbout" runat="server"/>
</Items>

then in your codebehind you can program against them and set enabled = false

page load
If (Userrole==Something)
{
    mnuAbout.Enabled = false;
}


The most feasible solution will be security trimming. Simple and understandable. Read all about it here

Mark as answer if you found it useful. I benefited from it. Let me know if you have any doubts.

0

精彩评论

暂无评论...
验证码 换一张
取 消