登录后显示 ASP.Net 母版页侧边栏(在登录时保持显示)
我的主页上有以下侧边栏。它不是任何 ContentPlaceHolder 的一部分。
<div runat="server" 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>
我最初隐藏它。但我想显示它并在登录后保持显示。使用标准的 Web 应用程序登录页面。我尝试了以下操作:
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;
}
我不确定是否与 div 标签交互,因为没有 Div 对象。无论如何,这不会显示带有菜单
编辑的侧边栏: 我最终将以下代码添加到母版页本身。
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 have the following sidebar on my master page. It is not a part of any ContentPlaceHolder.
<div runat="server" 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;
}
}
}
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
我会根据身份验证状态直接在您的 div 上设置可见性,
这样您就不需要
LoginUser_LoggedIn
方法,并且菜单将在每次加载时显示/隐藏,具体取决于用户登录或不记得从
控件中删除Visible="false"
,如果外部div
被隐藏,则什么也没有无论如何,里面都会显示出来。I would go for setting the visibiliy directly on your div based on authentication status
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 notAnd remember to remove the
Visible="false"
from your<asp:Menu>
control, if the outerdiv
is hidden, nothing inside it will be shown anyway.由于菜单的 Div 标签中有 runat="server" 标签,因此您可以直接在代码中引用它...
或者
Since you have the runat="server" tag in the menu's Div tag, you can reference it directly in code...
or
div 标签是一个 HtmlGenericControl 类。要访问此类,请导入命名空间 System.Web.UI.HtmlControls;并使用类似这样的内容:
或者您可以将菜单移至 UserControl 并隐藏或仅显示 UserControl 的使用 ID。
希望它能帮助您解决您的问题。
A div tag is a HtmlGenericControl class. To get access to this class import namespace System.Web.UI.HtmlControls; and use something like this:
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.