Friday, September 07, 2007

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

ASP.NET server control doesn't want toadd/display the value in a password textbox because of security concerns.
you have to add it yourself using an attribute.

Add this following line of code when you want set the value

TextBox1.Attributes.Add("value", "thePassword")
*( TextBox1 is a password textbox.)

While given solution will work, if a user does a view source onthe form, the password is visible as plain text and that is never goodfrom a security standpoint.

Rather, I would use Viewstate to hold the password value and if the userdoesn't change it then retrieve the value from view state.

Other Scenario is:-

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.

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

Enjoy


back to top