Welcome to AspAdvice Sign in | Join | Help

How to Prefill password into TextBox web control

This is one of the commonly asked question I have seen recently on ASP.NET forums. Logically speaking you shouldnt try to prefill a password textbox field for security reasons. But, if the application size and scope warrant a cheap trick to prefill password field, then keep reading....

Lets say you have a password “pwd“, which you retrived from database of session and you want to assign it to the .Text property of password textbox control. 

You would say passText.Text=“pwd“; and run the page. So you expect the password to be prefilled in the password field, when you see the actual page in the browser..But ASP.NET simply ignores it and display's a blank password field (Ofcourse that is exactly what it is designed to do)

Now lets say you want to find a workaround even if it means to compromise on the security.. keep reading..

As you know, we add client side properties with the Attributes.Add() method for web server controls. So to your pasword field.. add the password to the “value“ property. Remember a texbox is rendered as the INPUT html field and the .Text property will become “value“ property for the input control.

passText.Attributes.Add("value", “pwd“);

This will instruct the ASP.NET to assign the password to the value property of the html input control

Even here, as you can  see you are just manipulating the html version of this control. ASP.NET does not let you assign a value to a password field from the code behind


Caution: If you try to use this workaround.. keep in mind that anyone can see the password by looking at the html source of the rendered html

 

Sponsor
Published Sunday, June 26, 2005 2:14 PM by kreddi
Filed under:

Comment Notification

If you would like to receive an email when updates are made to this post, please register here

Subscribe to this post's comments using RSS

Comments

# PhoenixLog

Monday, March 17, 2008 9:12 AM by PhoenixLog

Leave a Comment

(required) 
required 
(required) 
Enter the code you see below