Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

Tags:

c#

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.Net before the page loads, but I can't get it to work.

like image 927
Cokegod Avatar asked Oct 19 '25 20:10

Cokegod


1 Answers

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.

like image 72
Kevin Babcock Avatar answered Oct 22 '25 10:10

Kevin Babcock



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!