Showing posts with label password. Show all posts
Showing posts with label password. Show all posts

Thursday, April 15, 2010

ASP.NET C# Set Value to Password Textbox in Code Behind

If ever you've tried setting value in code-behind to a textbox with TextMode property set to "Password", you would notice it doesn't display the masked text on the page. This is simply the way it is for security purposes, but there is a workaround so you can show the user there is actually text in the textbox.


Here is the snippet:


txt1.Text = "sample_password";


if (txt1.TextMode == TextBoxMode.Password) 
{
    txt1.Attributes.Add("value", txt1.Text);
}


Just set the same text value to the javascript attribute "value" and you're done.