How to keep/retain password value in the asp.net TextBox after postback event

Introduction: In this article i am going to explain with example How to maintain or preserve the password value in the TextBox controls during/on/while/even after the occurrence of post back event in asp.net using both C# and VB.Net languages. I have also explained how to clear maintained password on cancel or reset button click.

In previous articles i explained  How to Show hide password characters in textbox on checkbox check uncheck using jQuery and Create change password form in asp.net and Implement remember me next time checkbox option In login form and Recover and reset the forgot/lost password in using activation link in email and Using ajax password strength indicator in asp.net for password field 
Retain password in Asp.Net textbox even after postack

Description: While working on project i got the requirement to create a registration form having the TextBox for User Name, Password, Confirm Password and DropDownList for State and City.
The AutoPostback property of the State DropDownList was set to true so that corresponding cities can be populated in the city DropDownList based on the selected state.
But i faced an issue that whenever i select any State then post back occurs due to the AutoPostbBck propery of the DropDownList,  the entered values from the Password and Confirm Password TextBox get cleared. Actually it is the default nature of the TextBox control whose TextMode property is set to password.

But i want to maintain whatever password i have entered in the textbox across postback. Here in this article i have explained the solution for this problem.  Inside page load event i first checked whether it is postback, if yes then i am setting the value attribute of textbox with the value of password textbox. 

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

Note: I have not implemented the functionality of filling state and city from the database and just added the State and City statically. If you want so then read the articles How to Fill Country,State,Cities in the DropDownList  and Ajax CascadingDropDown to Fill DropDownList with Countries,states and cities 

Implementation: Let's create an asp.net sample website to see it in action.

In the design page (.aspx) design the page as:

HTML Source Code:

<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
    <title></title>
</head>
<body>
    <form id="form1" runat="server">
        <div>
            <fieldset style="width: 320px;">
                <legend>Maintain password after postback</legend>
                <table>                
                    <tr>
                        <td>UserName</td>
                        <td>
                            <asp:TextBox ID="txtUserName" runat="server"></asp:TextBox>
                        </td>
                    </tr>
                    <tr>
                        <td>Password</td>
                        <td>
                            <asp:TextBox ID="txtPwd" runat="server" TextMode="Password"></asp:TextBox>
                        </td>
                    </tr>
                    <tr>
                        <td>Confirm Password</td>
                        <td>
                            <asp:TextBox ID="txtConfirmPwd" runat="server" TextMode="Password"></asp:TextBox>
                        </td>
                    </tr>
                    <tr>
                        <td>State</td>
                        <td>
                            <asp:DropDownList ID="ddlState" Width="172px" runat="server" AutoPostBack="True">
                                <asp:ListItem Text="Select" Value="0"></asp:ListItem>
                                <asp:ListItem Text="Haryana" Value="1"></asp:ListItem>
                                <asp:ListItem Text="Punjab" Value="2"></asp:ListItem>
                                <asp:ListItem Text="Himachal" Value="3"></asp:ListItem>
                            </asp:DropDownList>
                        </td>
                    </tr>
                    <tr>
                        <td>City</td>
                        <td>
                            <asp:DropDownList ID="ddlCity" Width="172px" runat="server">
                                <asp:ListItem Text="Select" Value="0"></asp:ListItem>
                                <asp:ListItem Text="Panchkula" Value="1"></asp:ListItem>
                                <asp:ListItem Text="Ambala" Value="2"></asp:ListItem>
                                <asp:ListItem Text="Kalka" Value="3"></asp:ListItem>
                            </asp:DropDownList>
                        </td>
                    </tr>
                    <tr>
                        <td></td>
                        <td>
                            <asp:Button ID="btnSubmit" runat="server" Text="Submit"
                                OnClick="btnSubmit_Click" />
                            <asp:Button ID="btnCancel" runat="server" Text="Cancel"
                                OnClick="btnCancel_Click" />
                        </td>
                    </tr>
                </table>
            </fieldset> 
        </div>
    </form>
</body>

</html>

 Asp.Net C# code to keep the password even after Postback
  • In the code behind file(.aspx.cs) write the code as :
protected void Page_Load(object sender, EventArgs e)
    {
        if (IsPostBack)
        {
            if (!(String.IsNullOrEmpty(txtPwd.Text.Trim())))
            {
                txtPwd.Attributes["value"] = txtPwd.Text;
                //or txtPwd.Attributes.Add("value",txtPwd.Text);
            }
            if (!(String.IsNullOrEmpty(txtConfirmPwd.Text.Trim())))
            {
                txtConfirmPwd.Attributes["value"] = txtConfirmPwd.Text;
                //or txtConfirmPwd.Attributes.Add("value", txtConfirmPwd.Text);
            }
        }
    }

    protected void btnSubmit_Click(object sender, EventArgs e)
    {
        // Write your code here;     
    }

    protected void btnCancel_Click(object sender, EventArgs e)
    {
        txtUserName.Text = txtPwd.Text = txtConfirmPwd.Text = string.Empty;
        txtPwd.Attributes.Remove("value");
        txtConfirmPwd.Attributes.Remove("value");
        ddlState.ClearSelection();
        ddlCity.ClearSelection();       

    }


 Asp.Net VB Code keep the password even after Postback
  • In the code behind file(.aspx.vb) write the code as:
Protected Sub Page_Load(sender As Object, e As EventArgs) Handles Me.Load
        If IsPostBack Then
            If Not ([String].IsNullOrEmpty(txtPwd.Text.Trim())) Then
                'or txtPwd.Attributes.Add("value",txtPwd.Text);
                txtPwd.Attributes("value") = txtPwd.Text
            End If
            If Not ([String].IsNullOrEmpty(txtConfirmPwd.Text.Trim())) Then
                'or txtConfirmPwd.Attributes.Add("value", txtConfirmPwd.Text);
                txtConfirmPwd.Attributes("value") = txtConfirmPwd.Text
            End If
        End If
    End Sub

    Protected Sub btnSubmit_Click(sender As Object, e As EventArgs) Handles btnSubmit.Click
        ' Write your code here;     
    End Sub

    Protected Sub btnCancel_Click(sender As Object, e As EventArgs) Handles btnCancel.Click   
        txtUserName.Text = String.Empt
        txtPwd.Text= String.Empty
        txtConfirmPwd.Text= String.Empty
        txtPwd.Attributes.Remove("value")
        txtConfirmPwd.Attributes.Remove("value")
        ddlState.ClearSelection()
        ddlCity.ClearSelection()

    End Sub

Now over to you:
A blog is nothing without reader's feedback and comments. So please provide your valuable feedback so that i can make this blog better and If you like my work; you can appreciate by leaving your comments, hitting Facebook like button, following on Google+, Twitter, Linkedin and Pinterest, stumbling my posts on stumble upon and subscribing for receiving free updates directly to your inbox . Stay tuned and stay connected for more technical updates.        
Previous
Next Post »

3 comments

Click here for comments
Unknown
admin
January 26, 2016 ×

Useful document.it could help us to keep things in mind.

Reply
avatar
Unknown
admin
January 26, 2016 ×

Useful document.it could help us to keep things in mind.

Reply
avatar
February 08, 2016 ×

Thanks for you feedback..I am glad you liked this article..stay connected and keep reading for more useful updates like this.

Reply
avatar

If you have any question about any post, Feel free to ask.You can simply drop a comment below post or contact via Contact Us form. Your feedback and suggestions will be highly appreciated. Also try to leave comments from your account not from the anonymous account so that i can respond to you easily..