在 ASP.NET 页面中选择标头

发布于 2024-08-20 18:27:17 字数 270 浏览 5 评论 0原文

我创建了一个母版页 (Site.master),其中包含显示页眉、页脚和侧边栏的代码。它工作得非常好,但我无法弄清楚如何动态选择标题。

基本上,有两种可能的标头选项。如果用户未登录,我希望他们看到一个登录框和用于恢复密码的链接等。如果他们登录了,他们会看到一个注销链接,以及有关其帐户的一些信息(类似于如何实际上有效)。

是否可以让 Site.master 根据用户的登录状态检查并使用我想要的标头?我非常不知道从哪里开始(我想也许在母版页的代码隐藏中进行了一些检查),所以任何帮助将不胜感激。

I have created a master page (Site.master) which contains the code to display a header, a footer, and a side bar. It works really great, but I am having trouble figuring out how to dynamically choose the header.

Basically, there are two possible header options. If the user is not logged in, I want them to see a login box and links for recovering their password, etc. If they are logged in, they'll see a logout link, and some information about their account (similar to how SO works, actually).

Is it possible to have the Site.master check and use whichever header I want depending on the login status of the user? I'm pretty stuck on where to begin with this (I thought maybe some checks in the code-behind of the master page) so any help would be appreciated.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

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

发布评论

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

评论(4

潜移默化 2024-08-27 18:27:17

您应该考虑使用内置控件 LoginView (MSDN)。它专门设计用于为经过身份验证的用户和匿名用户提供多个模板(视图)。

这是一种最佳实践方法。您可以为登录和匿名用户定义页眉/页脚等,并使用适当的登录/注销按钮、用户信息等。

这是一个非常基本的示例:

<asp:LoginView id="LoginView1" runat="server">
    <AnonymousTemplate>
        <asp:HyperLink ID="lnkLogin" runat="server" NavigateUrl="~/Login.aspx" Text="Login"/>
    </AnonymousTemplate>
    <LoggedInTemplate>
        You are logged in as: <asp:LoginName id="lnCurrentUser" runat="server" />.
    </LoggedInTemplate>
</asp:LoginView>

.NET 框架将处理其余部分,并显示正确的模板无需任何额外代码。如果您最终在应用程序中使用多个角色,您可以更进一步并为这些角色定义模板(管理员与普通用户等)。

基于上述问题的完美解决方案: 如何:向匿名用户和登录用户显示不同的信息

You should consider using the built-in control, LoginView (MSDN). It is specifically designed to provide multiple templates (views), for authenticated and anonymous users.

This is a best practice approach. You can define your headers/footers etc. for logged in and anonymous users, with appropriate login/logout buttons, user information, etc. etc.

Here's a very basic example:

<asp:LoginView id="LoginView1" runat="server">
    <AnonymousTemplate>
        <asp:HyperLink ID="lnkLogin" runat="server" NavigateUrl="~/Login.aspx" Text="Login"/>
    </AnonymousTemplate>
    <LoggedInTemplate>
        You are logged in as: <asp:LoginName id="lnCurrentUser" runat="server" />.
    </LoggedInTemplate>
</asp:LoginView>

The .NET framework will handle the rest, and display the correct template without any extra code. If you end up using multiple roles in your application you can take this one step further and define templates for these roles as well (administrator vs regular user, etc.)

A perfect solution to your question based on the above: How to: Display Different Information to Anonymous and Logged In Users

雪化雨蝶 2024-08-27 18:27:17

是的,非常容易,只需将两个可能的标题放入其自己的面板控件中,然后在 Page_Load 中输入以下内容即可:

if ( Request.IsAuthenticated )
{
    // Display
    pnlAuthenticated.Visible = true;
    pnlGuest.Visible = false;
} 
else 
{
    // Display
    pnlAuthenticated.Visible = false;
    pnlGuest.Visible = true;
}

Yes, very easily by putting the two possible headers in their own Panel controls and just saying the following in the Page_Load:

if ( Request.IsAuthenticated )
{
    // Display
    pnlAuthenticated.Visible = true;
    pnlGuest.Visible = false;
} 
else 
{
    // Display
    pnlAuthenticated.Visible = false;
    pnlGuest.Visible = true;
}
权谋诡计 2024-08-27 18:27:17

就个人而言,我会将每组标题控件放入 2 个不同的占位符中,默认情况下将两者设置为不可见,

然后在母版页中使用一些代码

PlaceHolder1.Visible = Context.User.IsAuthenticated
PlaceHolder2.Visible = !Context.User.IsAuthenticated

Personally I would put each set of header controls into 2 different placeholders and by default set both to invisible

Then with some code in Master Page

PlaceHolder1.Visible = Context.User.IsAuthenticated
PlaceHolder2.Visible = !Context.User.IsAuthenticated
中性美 2024-08-27 18:27:17

是的,有两种方法;将标题嵌入面板中,并根据登录状态(在代码中发生)显示/隐藏面板。或者,您可以使用两个母版页,并在 OnPreInit 方法(或 PreInit 事件处理程序)中执行此检查,然后切换以显示要使用的母版页(您只能在此事件处理程序中以编程方式更改母版页)。

第二个选项的问题是 HttpContext.Current.user 在 PreInit...

HTH 中可能不可用。

Yes two ways of doing it; embed the header in a panel, and show/hide the panel depending on login status (which occurs in code). Alternatively, you could use two master pages, and do this check in the OnPreInit method (or PreInit event handler) and switch to show which master page you want to use (you can only change master pages programmatically in this event handler).

The problem with the second option is that HttpContext.Current.user may not be available in PreInit...

HTH.

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