May 23
ASP.NET and Password TextBox with initial value
Some asked me a question the other day about an issue they were experiencing with pre-loading a password text box with some initial text. Here is what their code looked like:
ASPX
<asp:TextBox id="txtPassword" runat="server" TextMode="Password" />
Code-Behind
txtPassword.Text = "mypassword";
But, when the page loaded, there was no value contained in the password text box. So, what’s the issue? Apparently, this is a restriction that the ASP.NET developers thought that should be in place due to security reasons. However, a workaround for this is to add the value to the attributes collection of the web server control:
txtPassword.Attributes.Add("value", "mypassword");
Doing this would cause the Value attribute of the <input type="password"> HTML control to be populated. So, here is what will be sent to the browser for rendering:
<input type="password" id="txtPassword" name="txtPassword" value="mypassword" />
It should be noted that even though the value of the password will be masked in the browser, the value will show up in clear text if you view the page source. So, if this is not the behavior you would like to have, then this is not the solution for you. Hopefully, this was of some help to you.
Until next time…
Happy Coding!
