JasCav
JasCav

Reputation: 34632

Choose Header in ASP.NET Page

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.

Upvotes: 5

Views: 1485

Answers (4)

Simon Mark Smith
Simon Mark Smith

Reputation: 1170

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

Upvotes: 1

KP.
KP.

Reputation: 13730

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

Upvotes: 3

Brian Mains
Brian Mains

Reputation: 50728

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.

Upvotes: 0

Keith Adler
Keith Adler

Reputation: 21178

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;
}

Upvotes: 2

Related Questions