ARTICLE

Disable Current password field in aspx ChangePassword control

Introduction

The ASP.NET ChangePassword control is a handy server-side control that provides a ready-made UI for users to update their passwords. By default, it always asks for the current password, new password, and confirm password.

But in some scenarios (for example, when an administrator resets a password for a user, or when migrating accounts from another system), you may want to remove or disable the Current Password field.

In this post, I’ll show you how to do that step by step.

Default ChangePassword Control

When you drag and drop the control, it usually looks like this:

<asp:ChangePassword ID="ChangePassword1" runat="server">
</asp:ChangePassword>

By default, it renders three textboxes:

  • Current Password
  • New Password
  • Confirm New Password

Why Disable Current Password?

Some common use cases:

  • Admin reset page: Admin is allowed to reset passwords without knowing the old one.
  • Forced password change: First-time login users must set a new password.
  • Password recovery flow: After OTP/email verification, old password check is unnecessary.

Solution:

In aspx page, write stylesheet for first row of the aspx:ChangePassword1 control

<style type="text/css">
    #ChangePassword1 table tr:nth-child(2){
        display:none;
    }
</style>

In cs page, add a function

public static Control FindControlRecursive(Control Root, string Id)
{
    if (Root.ID == Id)
        return Root;

    foreach (Control Ctl in Root.Controls)
    {
        Control FoundCtl = FindControlRecursive(Ctl, Id);
        if (FoundCtl != null)
            return FoundCtl;
    }

    return null;
}

In cs page, where the control should be shown,

TextBox CurrentPassword = (TextBox)FindControlRecursive(ChangePassword1, "CurrentPassword");
if (CurrentPassword != null)
{
    CurrentPassword.Attributes["value"] = "a_random_password";
}

The random password should be different from New Password textbox to allow the triggering of OnChangingPassword event.

Now it is done! Cheers!!!

NEWSLETTER

Subscribe to the Algolassi newsletter

Get practical .NET, C#, Blazor, SQL Server and developer tips in your inbox. No spam, just useful updates.

🤖 AlgoLassi Assistant Have a question about this tutorial?

Ask AlgoLassi and get an answer plus the tutorials worth studying next.

Ask a question

💬 Comments

Sign in with Google to publish immediately, or comment anonymously and wait for approval.

Comments will appear here when available.