简体   繁体   中英

Changing ContentPlaceHolder's content dynamically

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 ?

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!!

I would wrap your options in asp:Panel s 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. Eg: <p ID="loginLinks" runat="server" class="header-link">...etc...</p> where you could change the visibility with loginLinks.Visible

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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM