ASP.Net Master Page Sidebar display after Login (keeping it displayed whilst logged in)
I have the following sidebar on my master page. It is not a part of any ContentPlaceHolder.
<div runat="serv开发者_如何学运维er" visible="false" id="menuAccountMembersDiv" class="leftCol">
<asp:Menu ID="menuAccountMembers" runat="server" StaticSubMenuIndent="16px" Visible="false">
<Items>
<asp:MenuItem ImageUrl="~/Resources/x.png"
NavigateUrl="~/About.aspx" Text="x" ToolTip="x"
Value="b647ce4e-5c7f-400c-a921-ec7902494f26"></asp:MenuItem>
<asp:MenuItem ImageUrl="~/Resources/y.png"
NavigateUrl="~/About.aspx" Text="y" ToolTip="y"
Value="y"></asp:MenuItem>
<asp:MenuItem ImageUrl="~/Resources/sarahhunkin.png" NavigateUrl="~/About.aspx"
PopOutImageUrl="~/Resources/z.png" Text="z"
ToolTip="z" Value="z"></asp:MenuItem>
<asp:MenuItem ImageUrl="~/Resources/a.png"
NavigateUrl="~/About.aspx"
PopOutImageUrl="~/Resources/apop.png" Text="a"
ToolTip="a" Value="a"></asp:MenuItem>
</Items>
</asp:Menu>
</div>
I initially hide it. But I would like to display it and keep it displayed after logging in. Using the standard web application login page. I tried the following:
protected void LoginUser_LoggedIn(object sender, EventArgs e)
{
Menu MenuAccountMembers = (Menu)Master.FindControl("menuAccountMembers");
MenuAccountMembers.Visible = true;
Control menuAccountMembersDiv = (Control)Master.FindControl("menuAccountMembersDiv");
menuAccountMembersDiv.Visible = true;
}
I am not sure to to interact with the div tag, as there is no Div object. In any event, this does not display the sidebar with the menu
EDIT: I ended up adding the following code to the master page itself.
public partial class SiteMaster : System.Web.UI.MasterPage
{
protected void Page_Load(object sender, EventArgs e)
{
if (HttpContext.Current.Request.IsAuthenticated)
{
Control MenuDiv = this.FindControl("menuAccountMembersDiv");
MenuDiv.Visible = true;
Menu AccountMenu = (Menu)MenuDiv.FindControl("menuAccountMembers");
AccountMenu.Visible = true;
}
}
}
I would go for setting the visibiliy directly on your div based on authentication status
<div runat="server" visible="<%# Page.User.IsAuthenticated %>" id="menuAccountMembersDiv" class="leftCol">
that way you don't need your LoginUser_LoggedIn
method and the menu will show/hide on every load depending on the user is logged in or not
And remember to remove the Visible="false"
from your <asp:Menu>
control, if the outer div
is hidden, nothing inside it will be shown anyway.
Since you have the runat="server" tag in the menu's Div tag, you can reference it directly in code...
menuAccountMembersDiv.Style.Item("Display") = "none";
or
menuAccountMembersDiv.Visible = False;
A div tag is a HtmlGenericControl class. To get access to this class import namespace System.Web.UI.HtmlControls; and use something like this:
HtmlGenericControl div = Master.FindControl("menuAccountMembersDiv") as HtmlGenericControl;
if(div != null)
{
div.Visible = true;
}
Or you can move your menu to UserControl and hide or show just use ID of your UserControl.
Hope it will help you with your question.
精彩评论