ASP:TextBox Value disappears in postback only when password

JessicaB picture JessicaB · Jan 30, 2010 · Viewed 21.2k times · Source

I have an asp.net textbox like this:

 <asp:TextBox ID="PINPad" runat="server" Columns="6" MaxLength="4" 
      CssClass="PINTextClass"></asp:TextBox>

It is, as you might have guessed, the text box from an on screen PIN pad. Javascript fills in the values. The page is posted back every five seconds (using an update panel if that matters) to update various other unrelated items on the screen. This works just fine.

However, when I convert it to a password text box, like this:

  <asp:TextBox ID="PINPad" runat="server" Columns="6" MaxLength="4" 
       CssClass="PINTextClass" TextMode="Password"></asp:TextBox>

Then whenever the page posts back, the text box is cleared out on the screen and the textbox is empty (though during the timer event, the value does make it back to the server.)

Any suggestions how to fix this, so that it retains its value during postback?

Answer

bdukes picture bdukes · Jan 30, 2010

As a security feature, ASP.NET tries to disallow you from sending the password value back to the client. If you're okay with the security issues (i.e. it's either not really secure information or you're sure that the connection is secure), you can manually set the "value" attribute of the control, rather than using its Text property. It might look something like this:

this.PINPad.Attributes.Add("value", this.PINPad.Text);