Click here to Skip to main content
Licence 
First Posted 21 Mar 2007
Views 86,667
Downloads 864
Bookmarked 30 times

ASP.NET Password TextBox

By | 31 Mar 2007 | Article
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:

    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:

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

Now, you can use the PasswordTextBox as follows:

<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

About the Author

dknyoli

Team Leader

Germany Germany

Member



Sign Up to vote   Poor Excellent
Add a reason or comment to your vote: x
Votes of 3 or less require a comment

Comments and Discussions

 
You must Sign In to use this message board. (secure sign-in)
 
Search this forum  
 FAQ
    Noise  Layout  Per page   
  Refresh
QuestionCode distribution PinmemberMember 39884766:58 2 Mar '12  
GeneralUpdate PinmemberJeffMuir18:40 28 Jul '10  
GeneralPassword is shown PinmemberShahzad Raja22:34 11 Nov '08  
GeneralRe: Password is shown PinmemberGautam Jain22:20 29 Jan '09  
Generalthanks! PinmemberStephanie Spanjian10:27 14 May '08  
GeneralThanks Pinmemberserkansonmez3:13 17 Apr '08  
GeneralSource code missing PinmemberTony Bermudez8:44 31 Mar '07  
GeneralRe: Source code missing Pinmemberdknyoli7:09 1 Apr '07  
GeneralAJAX survival PinmemberVasudevan Deepak Kumar4:49 31 Mar '07  
GeneralThanks for feedback & update Pinmemberdknyoli0:56 31 Mar '07  
GeneralAlternatively.... PinmemberC Jones22:51 26 Mar '07  
GeneralRe: Alternatively.... Pinmemberjackmos2:35 27 Mar '07  
GeneralRe: Alternatively.... PinmemberC Jones2:57 27 Mar '07  
GeneralA simpler solution PinmemberRichard Deeming8:47 26 Mar '07  
GeneralNice Pinmembernsimeonov7:53 26 Mar '07  

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

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

Permalink | Advertise | Privacy | Mobile
Web03 | 2.5.120517.1 | Last Updated 31 Mar 2007
Article Copyright 2007 by dknyoli
Everything else Copyright © CodeProject, 1999-2012
Terms of Use
Layout: fixed | fluid