从没有特定角色的用户隐藏菜单

时间:2014-05-23 17:38:56

标签: c# asp.net

我想在用户未登录时隐藏文章菜单项, 母版页中的代码如下所示。

<asp:Menu ID="NavigationMenu" runat="server" CssClass="menu" EnableViewState="false" IncludeStyleBlock="false" Orientation="Horizontal">
    <Items>
        <asp:MenuItem NavigateUrl="~/Default.aspx" Text="Home"/>
        <asp:MenuItem NavigateUrl="~/Articles_List.aspx" Text="Articles" Value="Articles"/>
        <asp:MenuItem NavigateUrl="~/ItemsCategory.aspx" Text="Items" Value="Items"></asp:MenuItem>
        <asp:MenuItem NavigateUrl="~/Gallery.aspx" Text="Gallery" Value="Gallery"></asp:MenuItem>
        <asp:MenuItem NavigateUrl="~/Account/Register.aspx" Text="Registration" Value="Registration"></asp:MenuItem>
        <asp:MenuItem NavigateUrl="~/About.aspx" Text="About us" Value="About"></asp:MenuItem>
    </Items>
</asp:Menu>

我写了代码,但它没有用。

LoginView lv;

protected void Page_Load(object sender, EventArgs e)
{
    lv = (LoginView)Master.FindControl("HeadLoginView");
    if (!lv.Page.User.Identity.IsAuthenticated)
    {
        NavigationMenu.Items.RemoveAt(1);
    }   
}

1 个答案:

答案 0 :(得分:0)

try this 

<%if(User.Identity.IsAuthenticated){ %>
        <asp:Menu ID="NavigationMenu" runat="server" CssClass="menu" EnableViewState="false" IncludeStyleBlock="false" Orientation="Horizontal">
            <Items>
                <asp:MenuItem NavigateUrl="~/Default.aspx" Text="Home "/>
                <asp:MenuItem NavigateUrl="~/Articles_List.aspx" Text="Articles" Value="Articles" />
                <asp:MenuItem NavigateUrl="~/ItemsCategory.aspx" Text="Items" Value="Items" />
                <asp:MenuItem NavigateUrl="~/Gallery.aspx" Text="Gallery" Value="Gallery" />
                <asp:MenuItem NavigateUrl="~/Account/Register.aspx" Text="Registration" Value="Registration" />
                <asp:MenuItem NavigateUrl="~/About.aspx" Text="About us" Value="About" />
            </Items>
        </asp:Menu>
        <%}else{ %>
            <asp:Menu ID="Menu1" runat="server" CssClass="menu" EnableViewState="false" IncludeStyleBlock="false" Orientation="Horizontal">
                <Items>
                    <asp:MenuItem NavigateUrl="~/Default.aspx" Text="Home "/>
                    <asp:MenuItem NavigateUrl="~/ItemsCategory.aspx" Text="Items" Value="Items" />
                    <asp:MenuItem NavigateUrl="~/Gallery.aspx" Text="Gallery" Value="Gallery" />
                    <asp:MenuItem NavigateUrl="~/Account/Register.aspx" Text="Registration" Value="Registration" />
                    <asp:MenuItem NavigateUrl="~/About.aspx" Text="About us" Value="About" />
                </Items>
            </asp:Menu>
<%} %>