开发者

Set the value of an input tag with type="password" using C# and ASP.Net

So I have this input tag:

<input type="password" id="password" name="password" runat="server" />

I'm trying to set its value using C# and ASP.Ne开发者_高级运维t before the page loads, but I can't get it to work.


Don't set the input type in your markup or ASP.NET will clear the input before it is rendered. Instead, use the following markup:

<input id="tbPassword" name="password" runat="server" />

...and the following code in your code-behind:

tbPassword.Value = "your password";
tbPassword.Attributes["type"] = "password";

However, I strongly advise against this approach. For one, you shouldn't be storing passwords in plain text. It's a really bad security practice.

Two, if you're just going to set the password in code, why require it at all? The point of a password input is for user interaction (entering a password). If you do that for them, then having it on the page is pointless.


You can set the value of <asp:TextBox> using TextBox.Attributes.Add, then set the value property for the textbox.

code sample:

TextBox1.Attributes.Add("value", "My Sample Password");


This is by design for security reasons. What are you trying to fill the textbox with? You're not storing user's passwords in plain text are you?


If you're talking about a user login form, and you have access to the unencrypted password, you're doing it WRONG.

User passwords should be stored as a cryptographic hash in your database, which cannot be reversed into the plaintext password, only checked for a match.

If what you want to do is preload the user's password in the input box to save him some typing, just don't do it, because the user's web browser will probably do a better job at that.


Have you tried something like this:

void Page_Load(object sender, eventargs e)
{
   password.value= "something";
}
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜