20

If you have a page with an <asp:TextBox TextMode="Password" ... />.
How can you keep the value after a postback?

This is my problem:
At the registration screen of my app you need to enter a password. Then you click submit, a postback occurs and the password fields are cleared, how can I prevent the password field from clearing?

afuzzyllama
  • 6,538
  • 5
  • 47
  • 64
user29964
  • 15,740
  • 21
  • 56
  • 63
  • can i ask why would you want to do that? – ultrajohn Jun 30 '10 at 08:52
  • @user29964, This is an old post but, did you really accept that adding the password on the attribute=value solve your problem? It's a bad practice. If you go to view source code or inspect element on your password field after postback, everyone can see your password. – Maximus Decimus Jun 02 '15 at 20:16

7 Answers7

34

You require to set it again in page_load or in button click event like this :

 string Password = txtPassword.Text;
txtPassword.Attributes.Add("value", Password);
Pranay Rana
  • 175,020
  • 35
  • 237
  • 263
  • The title drew me here looking for a way to set this for an HTMLInputPassword field () but setting the value on this control doesn't work. Here's how you can set it for an actual HTML password input: `string script = String.Format("document.getElementById('{0}').value = '{1}';", TextBox1.ClientID, TextBox1.Value);` `Page.ClientScript.RegisterStartupScript(this.GetType(), "SetPassword", script, true);` – xr280xr Feb 28 '12 at 17:26
  • 6
    This approach is bad practice; you are appending to the client side control, so if you view source and look at the "value" attribute for the text box the password will be in clear text. Not a safe approach. Give this a read. http://www.codeproject.com/Articles/18927/How-to-safely-keep-a-password-field-during-postbac – Stefano D Oct 18 '12 at 20:54
  • It is blank on postback for me :/ – SearchForKnowledge May 11 '15 at 15:55
  • This exposes the password in the value attribute – Karim AG Aug 01 '16 at 12:51
13

You need to set back the password to the textbox on postback.

txtBox.Attributes["value"] = txtBox.Text;
Krunal
  • 3,443
  • 3
  • 23
  • 27
  • I tried this but it didn't work for me. http://stackoverflow.com/questions/30172411/why-is-the-password-field-not-being-retained-on-postback – SearchForKnowledge May 11 '15 at 16:15
3

Best way

dont set input type in aspx page, set type of input in Pageload in !postback Section

txtPassword.Attributes["type"] = "password";
Community
  • 1
  • 1
1

<input type="password" /> is treated differently than other form controls since it stores sensitive information that is a password of a user. At server side, for every postback the password textbox is force-fully cleared for this reason, should you really need to persist the value in password text-box, set it explicitly as others have mentioned here. But I really don't recommend doing so since it's not a good practice.

this. __curious_geek
  • 42,787
  • 22
  • 113
  • 137
1

Just add type="password" in asp:textbox and remove Textmode="Password" and no need to write any code in code-behind.

Kevin M. Mansour
  • 2,915
  • 6
  • 18
  • 35
0

I realize this is an old post, but hopefully this will help someone else. I had the same issue on a user setup screen whereby I kept losing the password input during various postbacks. The solution I chose was to place portion of the input screen that posted back into an updatepanel. This solved the problem of the password being blanked out AND didn't present a security risk.

Hope this helps!

0

Use Jquery to retain your password after postback or submit

$(function () {
$('.txtPassword').val("<%=txtPassword.Value%>");
});
Kabilan Smart
  • 166
  • 2
  • 4