Click here to Skip to main content
65,938 articles
CodeProject is changing. Read more.
Articles
(untagged)

Retain Password Field in ASP.NET Post Back

0.00/5 (No votes)
26 Apr 2010 1  
Normally, whenever a form is posted backed, the password field will clear. But we dont want it to do that. In that scenario, we can add a little bit of code will avoid the issue.Retain Password In VB.NETProtected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs)...
Normally, whenever a form is posted backed, the password field will clear. But we dont want it to do that.

In that scenario, we can add a little bit of code will avoid the issue.

Retain Password In VB.NET

VB
Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
If IsPostBack Then
            If Not String.IsNullOrEmpty(txtPassword.Text.Trim()) Then
                txtPassword.Attributes.Add("value", txtPassword.Text)
            End If
End If
End Sub


In C#
C#
protected void Page_Load(object sender, EventArgs e)
{

      if (IsPostBack)
      {
            if (!(String.IsNullOrEmpty(txtPassword.Text.Trim()))
            {
                  txtPassword.Attributes["value"]= txtPassword.Text;
            }
      }
}


There are many other methods also to retain values between postback, whether it is password or any field value stored in any control i.e. viewstate, session, etc

However retaining password is considered to be security hole so it is not preferable to retain password between post backs.

Happy coding

License

This article has no explicit license attached to it but may contain usage terms in the article text or the download files themselves. If in doubt please contact the author via the discussion board below.

A list of licenses authors might use can be found here