ChangePassword.DisplayUserName 属性

定义

获取或设置一个指示 ChangePassword 控件是否应显示 UserName 控件和标签的值。

public:
 virtual property bool DisplayUserName { bool get(); void set(bool value); };
public virtual bool DisplayUserName { get; set; }
member this.DisplayUserName : bool with get, set
Public Overridable Property DisplayUserName As Boolean

属性值

如果 ChangePassword 控件应显示 UserName,则为 true;否则为 false。 默认值为 false

示例

下面的代码示例演示如何设置 DisplayUserName 属性,以便向未登录网站的用户显示 UserName 控件。

<%@ page language="C#"%>

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
    "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<script runat="server">
  
  void Page_Load(object sender, EventArgs e )
  {
    if (Context.User.Identity.IsAuthenticated)
    {
      Changepassword1.DisplayUserName = false;
    }
  }
  
</script>

<html xmlns="http://www.w3.org/1999/xhtml" >
<head runat="server">
    <title>ChangePassword.DisplayUserName sample.</title>
</head>
<body>
    <form id="form1" runat="server">
    <div>
      User's login status: <asp:loginstatus id="status" runat="server" /><br />
      <asp:changepassword id="Changepassword1" runat="server" displayusername="true" />
    </div>
    </form>
</body>
</html>
<%@ page language="VB"%>

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
    "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<script runat="server">
  
  Sub Page_Load(ByVal sender As Object, ByVal e As EventArgs)
    If Context.User.Identity.IsAuthenticated Then
      changepassword1.DisplayUserName = False
    End If
  End Sub

</script>

<html xmlns="http://www.w3.org/1999/xhtml" >
<head runat="server">
    <title>ChangePassword.DisplayUserName sample.</title>
</head>
<body>
    <form id="form1" runat="server">
    <div>
      User's login status: <asp:loginstatus id="status" runat="server" /><br />
      <asp:changepassword id="Changepassword1" runat="server" displayusername="true" />
    </div>
    </form>
</body>
</html>

下面的代码示例演示如何使用 ASP.NET 页,该页使用 ChangePassword 控件,并包含名为 ChangingPassword的事件的ChangingPassword处理程序。 处理程序中的 ChangingPassword 代码将 存储在 属性中的 CurrentPassword 旧密码与 存储在 中的 NewPassword新密码进行比较。 如果两个密码相同,则更改密码会失败。

控件 ChangePasswordDisplayUserName 属性设置为 true ,使用户能够输入其用户名。 这意味着用户无需登录就可查看页面。

该代码示例假定 ASP.NET 网站已配置为使用 ASP.NET 成员身份和 Forms 身份验证,并且已创建一个名称和密码已知的用户。 有关详细信息,请参阅 如何:实现简单窗体身份验证

<%@ Page Language="C#" AutoEventWireup="True" %>

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
    "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<script runat="server">

  void Page_Load(Object sender, EventArgs e)
  {
    //Manually register the event-handling methods.
    ChangePassword1.ChangingPassword += new LoginCancelEventHandler(this._ChangingPassword);
  }

  void _ChangingPassword(Object sender, LoginCancelEventArgs e)
  {
    if (ChangePassword1.CurrentPassword.ToString() == ChangePassword1.NewPassword.ToString())
    {
      Message1.Visible = true;
      Message1.Text = "Old password and new password must be different.  Please try again.";
      e.Cancel = true;
    }
    else
    {
      //This line prevents the error showing up after a first failed attempt.
      Message1.Visible = false;
    }
  }

</script>

<html xmlns="http://www.w3.org/1999/xhtml" >
<head runat="server">
  <title>ChangePassword including a ChangingPassword event handler</title>
</head>
<body>
  <form id="form1" runat="server">
  <div style="text-align:center">

    <h1>ChangePassword</h1>
    
    <asp:LoginView ID="LoginView1" Runat="server" 
      Visible="true">
      <LoggedInTemplate>
        <asp:LoginName ID="LoginName1" Runat="server" FormatString="You are logged in as {0}." />
        <br />
      </LoggedInTemplate>
      <AnonymousTemplate>
        You are not logged in
      </AnonymousTemplate>
    </asp:LoginView><br />
    
    <asp:ChangePassword ID="ChangePassword1" Runat="server"
      BorderStyle="Solid" 
      BorderWidth="1" 
      CancelDestinationPageUrl="~/Default.aspx" 
      DisplayUserName="true" 
      OnChangingPassword="_ChangingPassword"
      ContinueDestinationPageUrl="~/Default.aspx" >
    </asp:ChangePassword><br />
  
    <asp:Label ID="Message1" Runat="server" ForeColor="Red" /><br />

    <asp:HyperLink ID="HyperLink1" Runat="server" 
      NavigateUrl="~/Default.aspx">
      Home
    </asp:HyperLink>
    
  </div>
  </form>
</body>
</html>
<%@ Page Language="VB" AutoEventWireup="True" %>

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">

<script runat="server">

  Public Sub PageLoad(ByVal Sender As Object, ByVal e As EventArgs)
    'Manually register the event-handling methods.
    AddHandler ChangePassword1.ChangingPassword, AddressOf Me._ChangingPassword
  End Sub

  Public Sub _ChangingPassword(ByVal Sender As Object, ByVal e As LoginCancelEventArgs)
    If (ChangePassword1.CurrentPassword.ToString() = ChangePassword1.NewPassword.ToString) Then
      Message1.Visible = True
      Message1.Text = "Old password and new password must be different.  Please try again."
      e.Cancel = True
    Else
      'This line prevents the error showing up after a first failed attempt.
      Message1.Visible = False
    End If
  End Sub

</script>

<html xmlns="http://www.w3.org/1999/xhtml" >
<head runat="server">
  <title>ChangePassword including a ChangingPassword event handler</title>
</head>
<body>
  <form id="form1" runat="server">
  <div style="text-align:center">

    <h1>ChangePassword</h1>
    
    <asp:LoginView ID="LoginView1" Runat="server" 
      Visible="true">
      <LoggedInTemplate>
        <asp:LoginName ID="LoginName1" Runat="server" FormatString="You are logged in as {0}." />
        <br />
      </LoggedInTemplate>
      <AnonymousTemplate>
        You are not logged in
      </AnonymousTemplate>
    </asp:LoginView><br />
    
    <asp:ChangePassword ID="ChangePassword1" Runat="server"
      BorderStyle="Solid" 
      BorderWidth="1" 
      CancelDestinationPageUrl="~/Default.aspx" 
      DisplayUserName="true" 
      OnChangingPassword="_ChangingPassword"
      ContinueDestinationPageUrl="~/Default.aspx" >
    </asp:ChangePassword><br />
  
    <asp:Label ID="Message1" Runat="server" ForeColor="Red" /><br />

    <asp:HyperLink ID="HyperLink1" Runat="server" 
      NavigateUrl="~/Default.aspx">
      Home
    </asp:HyperLink>
    
  </div>
  </form>
</body>
</html>

注解

若要更改其密码,必须由成员资格提供程序对用户进行身份验证。 若要允许未登录的用户更改其密码,或者由成员资格提供程序使用其他用户帐户进行身份验证,然后更改该帐户的密码,控件 ChangePassword 可以显示一个 TextBox 控件来接受用户名。

如果将ChangePassword控件显示给未登录的用户,则必须将 属性true设置为 DisplayUserName ;否则,用户将无法指定用户名。

适用于

另请参阅