Assign Value to Asp.Net TextBox with mode="Password"

Posted by in ASP.NET category on for Beginner level | Views : 15233 red flag

This article explains to assign value to the text box with "Password" mode, as text box with Password mode lost it's value on postback.

ASP.NET server control doesn't add/display the value in a password textbox because of security concerns. On most of the web sites at the time of registration password is asked. You must have noticed after submitting the page if you are again redirected to the page due to unsuccessful registration value of password field is lost.This is due to security concerns as everything is gets rendered on the client side So when you view the source in the browser password value will be there.

Solution 1

Add an attribute to textbox.Add this following line of code when you want set the value

TextBox1.Attributes.Add("value", "thePassword")
Above solution will work, but if a user does view source on the form, the password is visible as plain text and that is never good from a security point of view. Rather, I would use Viewstate to hold the password value and if the user doesn't change it then retrieve the value from view state.

Solution 2

But if explicitly you want to set the value of password text box again then use following method in .NET 2.0

txtPassword.Attributes["value"] = Request.Form[txtPassword.ClientID]; //in C#.Net 2.0
Page copy protected against web site content infringement by Copyscape

About the Author

Full Name: Virendra Dugar
Member Level: Silver
Member Status: Member,MVP
Member Since: 8/11/2009 4:14:05 AM
Country: India
Virendra Dugar is experienced Senior Software Developer with over 5 years of hands-on experience working with Microsoft .NET technology (ASP.NET, C#, VB.NET,SQL Server). He is always keen to learn new technology. He holds a Master's Degree in Computer Application & Information technology from Gujarat University in india.In free time, he loves to listen music, read books, play games and do blogging etc. Visit his blogs :

Login to vote for this post.

Comments or Responses

Login to post response

Comment using Facebook(Author doesn't get notification)