ASP.NET Password TextBox loses value after Postback
After creating a registration wizard, the password field was not being saved. After tracing the problem, it was found that the TextBox for the password was losing its value when additional postbacks occur prior to processing.
The first page of the wizard prompts for the generic user information such as name, email, and password. A postback is performed and a secondary wizard page prompts for some additional information. When the wizard's finish method is called, we store all of the information.
The fix is simple. The password's "Text" property is not stored in ViewState (we're assuming for some security reason). We counter this by simply setting (and reading from) the TextBox's attributes.
You can leave your ASP.NET control as it is. In the page's Load event, simply add an attribute to the Password TextBox's and read this attribute instead of the "Text" property when you want to retrieve it.
{
// Only set on postback where the txtPassword.Text property has a value
if(IsPostBack && txtPassword.Text != string.Empty)
{
txtPassword1.Attributes["value"] = txtPassword.Text;
}
}
Finally, when you want to retrieve the value, just access the attribute instead of the Text property.
{
if (Page.IsValid)
{
Ramsay.CoreLibraries.PendingAccount pa = new Ramsay.CoreLibraries.PendingAccount();
pa.Password = txtPassword.Attributes["value"];
pa.Save();
}
}
Recent blog posts
- Canned VirtualPC Instances for IE 6, 7, 8 on XP/Vista
- Checking assembly dependencies for .NET
- Google's Public DNS
- Server Utility Functions for Non-Web Apps
- reCAPTCHA for ASP.NET MVC that uses ModelState
- Adding a container to ValidationSummary helper in ASP.NET MVC
- Generic XML Serialization Class
- Re-throwing Exceptions in C# with InternalPreserveStackTrace
- Solving xsd generation error: 'The element .... is missing'
- Enum DropDownList in ASP.NET MVC
Recent comments
- so very helpful kenny. thank
9 weeks 2 days ago - Sorry, apparently the drupal
12 weeks 3 days ago - A better method
15 weeks 4 days ago - No Source Code?
21 weeks 2 days ago - security
31 weeks 1 day ago - Nice simple solution
1 year 15 weeks ago - That's quite an interesting
1 year 32 weeks ago - Small Complaint
1 year 33 weeks ago - NIceeee
1 year 39 weeks ago - Both the SQL Server connector
2 years 16 weeks ago
Comments
#1 Anonymous
You will have to rely on a secure connection, by putting the value in "txtPassword1.Attributes["value"] = txtPassword.Text;" your password is view able in the page source. Which could be kinda bad.
#2 Anonymous
Wish I'd found this sooner, very straight forward.
#3 Anonymous
Saved my time and energy. ONly if google could put it on top...
#4 Anonymous
A better method would be to create a property that saves the password in the encrypted viewstate like so:
Private Property Password As String
Get
Return CType(ViewState("Password"), String)
End Get
Set(value As String)
ViewState("Password") = value
End Set
End Property
Then we can set the password on postback like so:
Me.Password = textboxpassword.Text
And we can read the password on subsequent postbacks like so:
thepassword = Me.Password