Populate password field in ASP.NET from code-behind

Posted by: Third  :  Category: ASP.NET

If you have a password field in ASP.NET (i.e. TextBox with TextMode property equal to password) and you want to populate the field with masked password value, assigning value using the Text property will not work.

<asp:TextBox ID="txtPassword" runat="server" TextMode="Password"></asp:TextBox>

Instead you have to set the value of the field by adding the ‘value’ attribute to your field.

In C#,

txtPassword.Attributes.Add("value", yourpasswordvalue);

In VB.Net

txtPassword.Attributes.Add("value", yourpasswordvalue)

In Delphi.Net

txtPassword.Attributes.Add('value', yourpasswordvalue);

Leave a Reply