Pages

Tuesday 9 April 2013

Retain Password Field value in ASP.NET Post Back

Retain Password Field in ASP.NET Post Back
Retain Password Field value in ASP.NET Post Back

In asp.net when you submit a form using button or some control like image button Post Back occurs . if form is lengthy and if some error occurs when user press back button of browser all other fields in form remain filled with information if you have ViewState enabled for the text boxes but password field loose the values inside it.

So ,we want that password field also retain its value when post back occurs .To retain value of password field in PageLoad event we need to place the Value of password field into value attribute of the Password TextBox.
protected void Page_Load(object sender, EventArgs e)
{
    if (IsPostBack)
    {
        if (!String.IsNullOrEmpty(txtPassword.Text))
        {
            txtPassword.Attributes.Add("value", txtPassword.Text);
        }
    }
}

No comments:

Post a Comment