登录后显示 ASP.Net 母版页侧边栏(在登录时保持显示)

发布于 2024-10-06 23:31:29 字数 2055 浏览 0 评论 0原文

我的主页上有以下侧边栏。它不是任何 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

滥情哥ㄟ 2024-10-13 23:31:29

我会根据身份验证状态直接在您的 div 上设置可见性,

<div runat="server" visible="<%# Page.User.IsAuthenticated %>" id="menuAccountMembersDiv" class="leftCol">

这样您就不需要 LoginUser_LoggedIn 方法,并且菜单将在每次加载时显示/隐藏,具体取决于用户登录或不

记得从 控件中删除 Visible="false",如果外部 div 被隐藏,则什么也没有无论如何,里面都会显示出来。

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.

分分钟 2024-10-13 23:31:29

由于菜单的 Div 标签中有 runat="server" 标签,因此您可以直接在代码中引用它...

menuAccountMembersDiv.Style.Item("Display") = "none";

或者

menuAccountMembersDiv.Visible = False;

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;
橘虞初梦 2024-10-13 23:31:29

div 标签是一个 HtmlGenericControl 类。要访问此类,请导入命名空间 System.Web.UI.HtmlControls;并使用类似这样的内容:

HtmlGenericControl div = Master.FindControl("menuAccountMembersDiv") as HtmlGenericControl;
if(div != null)
{
    div.Visible = true;
}

或者您可以将菜单移至 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:

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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文