Click here to Skip to main content
15,884,629 members
Articles / Web Development / ASP.NET
Article

ASP.NET Password TextBox

Rate me:
Please Sign up or sign in to vote.
4.43/5 (21 votes)
31 Mar 20071 min read 215.3K   2K   34   16
A password textbox that survives postback and that can be set programmatically.

Introduction

Using ASP.NET, the default textbox in password mode (TextMode="Password") has the following restrictions:

  • The password can not be set (e.g., tbPassword.Text = "123";)
  • The password is not restored after a postback

Both restrictions make perfect sense for security reasons. Nonetheless, sometimes the above behaviour is not wanted (especially during development).

The new PasswordTextBox control is an extension of the standard ASP.NET TextBox and makes a password textbox behave like a regular textbox.

Using the code

The code itself is pretty straightforward:

C#
public class PasswordTextBox : TextBox
{
    public PasswordTextBox()
    {
        TextMode = TextBoxMode.Password;
    }

    public override string Text
    {
        get
        {
            return base.Text;
        }
        set
        {
            base.Text = value;

            Attributes["value"] = value;
        }
    }

    protected override void OnPreRender(EventArgs e)
    {
        base.OnPreRender(e);

        Attributes["value"] = Text;
    }
}

To use the code, just drop PasswordTextBox.dll in the /bin of your web application, or add a reference to it in Visual Studio.

ASP.NET 2.0 offers a nice and clean way to register controls for your aspx pages: you simply add the following to your web.config:

XML
<pages>
    <controls>
        <add tagPrefix="opp" assembly="PasswordTextBox" namespace="opp"/>
    </controls>
</pages>

Now, you can use the PasswordTextBox as follows:

HTML
<opp:PasswordTextBox id="tbPassword" runat="server" />

Points of interest

To set the value of the PasswordTextBox, the Text property is overriden and the value is set via the "value" attribute.

To restore the value of PasswordTextBox after a postback, the value is retrieved in the OnPreRender event and set via the "value" attribute (thanks to jackmos for this more nifty solution).

History

  • V1.0 Released 2007-03-21.
  • V1.1 Released 2007-03-31. Thanks for all the good feedback!

License

This article has no explicit license attached to it but may contain usage terms in the article text or the download files themselves. If in doubt please contact the author via the discussion board below.

A list of licenses authors might use can be found here


Written By
Team Leader
Germany Germany
Passionate about C#, .NET, SharePoint, iOS, Swift and Machine Learning.

Get your weekly SharePoint & Office 365 News: http://www.sharepointweekly.com/

Comments and Discussions

 
GeneralMy vote of 5 Pin
moseti21-Jul-12 0:58
moseti21-Jul-12 0:58 
QuestionCode distribution Pin
derekbh2-Mar-12 6:58
derekbh2-Mar-12 6:58 
GeneralUpdate Pin
JeffMuir28-Jul-10 18:40
JeffMuir28-Jul-10 18:40 
GeneralPassword is shown Pin
Shahzad Raja11-Nov-08 22:34
Shahzad Raja11-Nov-08 22:34 
GeneralRe: Password is shown Pin
Gautam Jain29-Jan-09 22:20
Gautam Jain29-Jan-09 22:20 
Generalthanks! Pin
Stephanie Spanjian14-May-08 10:27
Stephanie Spanjian14-May-08 10:27 
GeneralThanks Pin
serkansonmez17-Apr-08 3:13
serkansonmez17-Apr-08 3:13 
GeneralSource code missing Pin
Tony Bermudez31-Mar-07 8:44
Tony Bermudez31-Mar-07 8:44 
GeneralRe: Source code missing Pin
dknyoli1-Apr-07 7:09
dknyoli1-Apr-07 7:09 
GeneralAJAX survival Pin
Vasudevan Deepak Kumar31-Mar-07 4:49
Vasudevan Deepak Kumar31-Mar-07 4:49 
GeneralThanks for feedback &amp; update Pin
dknyoli31-Mar-07 0:56
dknyoli31-Mar-07 0:56 
GeneralAlternatively.... Pin
C Jones26-Mar-07 22:51
C Jones26-Mar-07 22:51 
GeneralRe: Alternatively.... Pin
jackmos27-Mar-07 2:35
professionaljackmos27-Mar-07 2:35 
GeneralRe: Alternatively.... Pin
C Jones27-Mar-07 2:57
C Jones27-Mar-07 2:57 
GeneralA simpler solution Pin
Richard Deeming26-Mar-07 8:47
mveRichard Deeming26-Mar-07 8:47 
It would be much simpler to override the AddAttributesToRender method instead:

C#
public class PasswordTextBox : TextBox
{
    public PasswordTextBox()
    {
    }
    
    [DefaultValue(TextBoxMode.Password)]
    public override TextBoxMode TextMode
    {
        get
        {
            object value = this.ViewState["Mode"];
            if (null != value) return (TextBoxMode)value;
            return TextBoxMode.Password;
        }
        set
        {
            if (!Enum.IsDefined(typeof(TextBoxMode), value))
            {
                throw new ArgumentOutOfRangeException("value");
            }
            
            this.ViewState["Mode"] = value;
        }
    }
    
    protected override void AddAttributesToRender(HtmlTextWriter writer)
    {
        base.AddAttributesToRender(writer);
        
        if (TextBoxMode.Password == this.TextMode)
        {
            writer.AddAttribute(HtmlTextWriterAttribute.Value, this.Text);
        }
    }
}



"These people looked deep within my soul and assigned me a number based on the order in which I joined." - Homer

GeneralNice Pin
nsimeonov26-Mar-07 7:53
nsimeonov26-Mar-07 7:53 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.