65.9K
CodeProject is changing. Read more.
Home

Retain Password Field in ASP.NET Post Back

starIconstarIcon
emptyStarIcon
starIcon
emptyStarIconemptyStarIcon

2.61/5 (41 votes)

Jan 2, 2010

CPOL
viewsIcon

47602

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
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#
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