Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

ASP:TextBox Value disappears in postback only when password

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?

like image 641
JessicaB Avatar asked Jan 30 '10 21:01

JessicaB


People also ask

How can we keep TextBox value after PostBack in asp net?

One of the common interview question is “Explain the use of ViewState for TextBox?” and the most popular and common answer for this question is view state will maintain the text property value of the TextBox after a PostBack of the page.

How can we keep password in PostBack in asp net?

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


2 Answers

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);
like image 69
bdukes Avatar answered Oct 22 '22 16:10

bdukes


protected void Page_Load(object sender, EventArgs e)
{
    if (IsPostBack)
  {
        if (!(String.IsNullOrEmpty(txtPwd.Text.Trim())))
        {
             txtPwd.Attributes["value"]= txtPwd.Text;              
        }
        if (!(String.IsNullOrEmpty(txtConfirmPwd.Text.Trim())))
        {
            txtConfirmPwd.Attributes["value"] = txtConfirmPwd.Text;
        }
  }
}
like image 22
user3859917 Avatar answered Oct 22 '22 15:10

user3859917