【问题标题】:Changing ContentPlaceHolder's content dynamically动态更改 ContentPlaceHolder 的内容
【发布时间】:2014-05-27 23:42:17
【问题描述】:

我有一个索引页面作为内容页面,在它的顶部我有一个这样的内容占位符。

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

我想在用户登录时更改其内容。登录后,我想输入用户名而不是这些链接。我已经有了登录方法,只是想知道如何在不使用其他内容页面的情况下进行此操作?还是对我来说这是一个合乎逻辑的解决方案?我应该使用另一个内容页面作为主页吗?

【问题讨论】:

标签: c# asp.net master-pages content-pages


【解决方案1】:

我会将您的选项包装在 asp:Panels 中或使用 LoginView 控件作为 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>

然后在你的代码后面

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

您不必使用面板。您还可以通过给它们一个 ID 和一个 runat="server" 属性来访问 HTML 元素并在代码隐藏中访问它们。例如:&lt;p ID="loginLinks" runat="server" class="header-link"&gt;...etc...&lt;/p&gt;,您可以使用 loginLinks.Visible 更改可见性

【讨论】:

  • 谢谢!但是如何在注销过程后防止返回按钮?
  • 如果您谈论的是浏览器的后退按钮,则无法可靠地完成。
【解决方案2】:

您可以在代码中使用集成的登录控件,将它们放在 contentplaceholder 上并自定义以显示用户名...

所以你放置一个 asp:LoginView,在它的 AnonymousTemplate 上你可以放置一个 asp:Login 控件并自定义它,在 LoggedInTemplate 上你可以放置登录名、注销链接、更改密码等。

这是一个例子:

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

该代码应该放在您的 ContentPlaceHolder 中

希望对你有用!!

【讨论】:

    【解决方案3】:

    有一些方法可以解决您的问题,具体取决于您的登录方式, 如果您使用的是 ASP.NET Forms 身份验证,那么您可以如下所示显示您的用户名:

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

    如果您想在用户未通过身份验证的情况下拥有这些链接,那么您可以在当前内容占位符中包含类似的内容:

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

    或者,如果您有自己的身份验证方法而不是 Context.User.Identity.IsAuthenticated,您可以从数据库中检查您的用户身份验证,并通过后面代码中的属性将其传递。

    【讨论】:

    • 是的,正如我所说,这完全取决于 OP 的会员设计,这只是基于他想要在他的页面中实现的目标。他也可以只使用登录控件,但这又是开发人员的选择。
    • 这可能是最简单但最糟糕的方法。将逻辑放在 aspx 页面中完全违背了页面背后代码的目的。
    • @JonP 不是始终使用代码的义务,使用内联代码没有任何问题,因为您可能会在MVC 应用程序遵循相同的策略。这与您设计应用程序和项目架构的方式有关。
    猜你喜欢
    • 2017-09-10
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2012-02-28
    • 1970-01-01
    • 2023-03-20
    • 1970-01-01
    相关资源
    最近更新 更多