In this article I will explain with an example, how to set TextBox value when TextMode is Password ASP.Net using C# and VB.Net.
By default when you set the Text property of a TextBox with TextMode property set to Password the value is not shown in the TextBox.
 
 
Security Reason
Retaining Password in ASP.Net Password TextBox will make browsers print the Password in the Page’s HTML and hence it is not recommended to use this technique for Login form.
 
Setting TextBox value when TextMode is Password ASP.Net
Though it is not recommended to retain values of Password TextBoxes across PostBacks on Login forms, still there are places where it could be really helpful such has Registration Form where users need to enter Password twice.
HTML Markup
The following HTML Markup consists of a normal TextBox, a Password TextBox and a Button.
<table border="0" cellpadding="0" cellspacing="0">
    <tr>
        <td>
            Username:
        </td>
        <td>
            <asp:TextBox ID="txtUserName" runat="server" />
        </td>
    </tr>
    <tr>
        <td>
            Password:
        </td>
        <td>
            <asp:TextBox ID="txtPassword" runat="server" TextMode="Password" />
        </td>
    </tr>
    <tr>
        <td>
            <asp:Button ID="btnSubmit" Text="Submit" runat="server" />
        </td>
    </tr>
</table>
 
Inside the Page Load event of the page, first a check is performed whether the page is performing a PostBack and if yes then the value of the Password TextBox is set using the HTML value attribute.
C#
protected void Page_Load(object sender, EventArgs e)
{
    if (this.IsPostBack)
    {
        txtPassword.Attributes["value"] = txtPassword.Text;
    }
}
 
VB.Net
Protected Sub Page_Load(sender As Object, e As EventArgs) Handles Me.Load
    If Me.IsPostBack Then
        txtPassword.Attributes("value") = txtPassword.Text
    End If
End Sub
 
 
Screenshot
Set TextBox value when TextMode is Password ASP.Net
 
 
Demo
 
 
Downloads