1

I have an index page as a content page and at the top of it i have a content place holder like this.

    <asp:Content ID="Content3" ContentPlaceHolderID="logProcessHolder" runat="server">
        <p class="header-link"><a href="LoginFormContainer.aspx">login</a>&nbsp;or&nbsp;<a href="RegisterFormContainer.aspx">create an account</a></p>
    </asp:Content>

I want to change its content when users login. After login i want to put user name instead of these links. I have my login method already, just want to know how can i make this without using an another content page ? or is it a logical solution for me ? Should i use an another content page as a home page ?

John Saunders
  • 160,644
  • 26
  • 247
  • 397
Tartar
  • 5,149
  • 16
  • 63
  • 104
  • 1
    I have edited your title. Please see, "[Should questions include “tags” in their titles?](http://meta.stackoverflow.com/questions/19190/)", where the consensus is "no, they should not". – John Saunders May 28 '14 at 00:38

3 Answers3

2

You can use the integrated login controls on your code, place them on the contentplaceholder and customize to show the users name...

So you place a asp:LoginView, and on its AnonymousTemplate you can place a asp:Login control and customize it, and on the LoggedInTemplate you can place the login name, a logout link, change password, etc.

Here's an example:

<asp:LoginView ID="LoginView1" runat="server">
    <AnonymousTemplate>
        <asp:Login ID="Login1" runat="server"></asp:Login>
    </AnonymousTemplate>
    <LoggedInTemplate>
        <asp:LoginName ID="LoginName1" runat="server" /> | <asp:LoginStatus ID="LoginStatus1" runat="server" LogoutAction="RedirectToLoginPage" LogoutText="End session" />
    </LoggedInTemplate>
</asp:LoginView>

That code should be place right inside your ContentPlaceHolder

Hope this is useful!!

epaezr
  • 454
  • 2
  • 6
  • 15
2

I would wrap your options in asp:Panels or use the LoginView control as epaezr has done

<asp:Content ID="Content3" ContentPlaceHolderID="logProcessHolder" runat="server">
   <asp:Panel ID="pnlAnonymous" runat="server">
    <p class="header-link"><a href="LoginFormContainer.aspx">login</a>&nbsp;or&nbsp;<a href="RegisterFormContainer.aspx">create an account</a></p>
   </asp:Panel>
   <asp:Panel ID="pnlVerified" runat="server">
       <asp:Literal ID="litUserName" runat="server" />
   </asp:Panel>
</asp:Content>

Then in your code behind

protected void Page_Load(object sender, EventArgs e)
{
    if (Request.IsAuthenticated)
    { 
        pnlAnonymous.Visible = false;
        pnlVerified.Visible = true;
        litUserName.Text = Context.User.Identity.Name;
    }
    else
    {            
        pnlAnonymous.Visible = true;
        pnlVerified.Visible = false;
    }
 }

You don't have to use panels. You could also HTML elements and access them in the code-behind by giving them an ID and a runat="server" attribute. E.g: <p ID="loginLinks" runat="server" class="header-link">...etc...</p> where you could change the visibility with loginLinks.Visible

Community
  • 1
  • 1
Jon P
  • 19,442
  • 8
  • 49
  • 72
1

there are some ways around your question depends on your login method, if your are using ASP.NET Forms Authentication then you could display your username just like below:

   <%: Context.User.Identity.Name %>

and if you wanna have those links if user is not authenticated then you could have something like this inside your current content placeholder:

  <asp:Content ID="Content3" ContentPlaceHolderID="logProcessHolder" runat="server">  >        <p class="header-link">
    <% if(Context.User.Identity.IsAuthenticated){ %>
         <div><%: Context.User.Identity.Name %></div>&nbsp;&nbsp;
         <a href="Logout.aspx">logout</a>
    <%} else { %>
         <a href="LoginFormContainer.aspx">login</a>&nbsp;or&nbsp;
         <a href="RegisterFormContainer.aspx">create an account</a>
    <%} %>  
   </p> 
  </asp:Content>

alternatively if you have your own method of authentication instead of Context.User.Identity.IsAuthenticated you could check your user's authentication from the database and pass it through a property from your code behind.

Ali
  • 2,574
  • 1
  • 17
  • 24
  • Yeah as I said it's totally depends on the OP's membership design, this is just based on what he wanted to achieve inside his page. he could just use Login control as well but again it's the developer's choice. – Ali May 28 '14 at 00:33
  • This is posibliy the easiest but worst way to do it. Putting logic in the aspx page totaly defeats the purpose of code behind pages. – Jon P May 28 '14 at 00:34
  • @JonP it is **NOT** an obligation to use code behind all the time and there is nothing wrong to use inline code, as you might find the presentation layer(View) in MVC applications is following the same strategy. IT IS ABOUT THE WAY YOU DESIGN YOUR APPLICATION AND YOUR PROJECT's ARCHITECTURE. – Ali May 28 '14 at 00:44