我想在用户未登录时隐藏文章菜单项, 母版页中的代码如下所示。
<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);
}
}
答案 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>
<%} %>