Was this page helpful?
Your feedback about this content is important. Let us know what you think.
Additional feedback?
1500 characters remaining
Export (0) Print
Expand All
Expand Minimize

ChangePassword.DisplayUserName Property

Note: This property is new in the .NET Framework version 2.0.

Gets or sets a value indicating whether the ChangePassword control should display the UserName control and label.

Namespace: System.Web.UI.WebControls
Assembly: System.Web (in system.web.dll)

public virtual bool DisplayUserName { get; set; }
/** @property */
public boolean get_DisplayUserName ()

/** @property */
public void set_DisplayUserName (boolean value)

public function get DisplayUserName () : boolean

public function set DisplayUserName (value : boolean)

Property Value

true if the ChangePassword control should display the UserName; otherwise, false. The default is false.

In order to change their password, users must be authenticated by the membership provider. To allow users who are not logged on to change their password, or to be authenticated by the membership provider with a different user account and then change the password for that account, the ChangePassword control can display a TextBox control to accept the user name.

You must set the DisplayUserName property to true if the ChangePassword control will be displayed to users who are not logged on; otherwise, the user will not be able to specify a user name.

The following code example shows how to set the DisplayUserName property to display the UserName control to users who are not logged on to the Web site.

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

<script runat="server">
  void Page_Load(object sender, EventArgs e )
    if (Context.User.Identity.IsAuthenticated)
      Changepassword1.DisplayUserName = false;

<head runat="server">
    <title>ChangePassword.DisplayUserName sample.</title>
    <form id="form1" runat="server">
      User's login status: <asp:loginstatus id="status" runat="server" /><br />
      <asp:changepassword id="Changepassword1" runat="server" displayusername="true" />

The following code example shows how to use an ASP.NET page that uses a ChangePassword control, and includes a handler for the ChangingPassword event named ChangingPassword. The code in the ChangingPassword handler compares the old password stored in the CurrentPassword property to the new password stored in NewPassword. If the two passwords are the same, changing the password fails.

The ChangePassword control sets the DisplayUserName property to true to enable the user to enter their user name. This means that the user does not have to log on to view the page.

The code example assumes that the ASP.NET Web site has been configured to use ASP.NET membership and Forms authentication, and that a user has been created whose name and password are known to you. For more information, see How to: Implement Simple Forms Authentication.

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

<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;
      //This line prevents the error showing up after a first failed attempt.
      Message1.Visible = false;


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

    <asp:LoginView ID="LoginView1" Runat="server" 
        <asp:LoginName ID="LoginName1" Runat="server" FormatString="You are logged in as {0}." />
        <BR />
        You are not logged in
    </asp:LoginView><br />
    <asp:ChangePassword ID="ChangePassword1" Runat="server"
      ContinueDestinationPageUrl="~/Default.aspx" >
    </asp:ChangePassword><br />
    <asp:Label ID="Message1" Runat="server" ForeColor="Red" /><br />

    <asp:HyperLink ID="HyperLink1" Runat="server" 

Windows 98, Windows 2000 SP4, Windows Server 2003, Windows XP Media Center Edition, Windows XP Professional x64 Edition, Windows XP SP2, Windows XP Starter Edition

The .NET Framework does not support all versions of every platform. For a list of the supported versions, see System Requirements.

.NET Framework

Supported in: 2.0

Community Additions

© 2015 Microsoft