How to use ASP.NET Membership ChangePassword control

You can use this control when you want to allow the user to change his/her password. The control simply queries the user name as well as the old password from the user. Then it requires the user to enter the new password and confirm the new password. You can use the control on a secured page as follows:

 

<asp:ChangePassword ID=”ChangePwdCtrl” runat=”server” BorderStyle=”solid” BackColor=”Orange”>

<MailDefinition From=”passwords@mydomain.com”

Subject=”Changes in your profile”

Priority=”high” />

<TitleTextStyle Font-Bold=”true” Font-Names=”Arial Narrow” ForeColor=”blue” />

</asp:ChangePassword>

 

The controls includes a MailDefinition child element with the same settings as the PasswordRecovery control. This is because after the password has been changed successfully, the control automatically can send an e-mail to the user’s e-mail address if a mail server is configured for the web application. You can customize through both properties and styles and a template-based approach. Two templates are required when customizing the control:

– The ChangePasswordTemplate displays the fields for entering the old user name and password as well as the new password, including the password confirmation field.

– The SuccessTemplate displays the success message, telling the user if the reset completed successfully or not.

 

The ChangePasswordTemplate requires you to add some special controls with special IDs and CommandName property values. You can find these control ID values and CommandName values in bold in the following code snippet:

 

<asp:ChangePassword ID=”ChangePwdCtrl” runat=”server”>

<ChangePasswordTemplate>

Old Password:&nbsp;

<asp:TextBox ID=”CurrentPassword” runat=”server”

TextMode=”Password” /><br />

New Password:&nbsp;

<asp:TextBox ID=”NewPassword” runat=”server”

TextMode=”Password” /><br />

Confirmation:&nbsp;

<asp:TextBox ID=”ConfirmNewPassword” runat=”server”

TextMode=”Password” /><br />

<asp:Button ID=”ChangePasswordPushButton” CommandName=”ChangePassword”

runat=”server” Text=”Change Password” />

<asp:Button ID=”CancelPushButton” CommandName=”Cancel”

runat=”server” Text=”Cancel” /><br />

<asp:Literal ID=”FailureText” runat=”server”

EnableViewState=”False” />

</ChangePasswordTemplate>

<SuccessTemplate>

Your password has been changed!

<asp:Button ID=”ContinuePushButton” CommandName=”Continue”

runat=”server” Text=”Continue” />

</SuccessTemplate>

</asp:ChangePassword>

 

 

The text box controls of the ChangePasswordTemplate are mandatory. The other controls are optional. If you select the ID properties and the CommandName properties for the buttons appropriately, you don’t have to write any additional code.