How to preserve the password value of Textbox during postback ?


While creating the registration page in, There will be so many field and we have to also keep “auto postback” on so many server controls on basis of our requirement. If there is password field and we have kept some value there , if postback will happen,then we will lose the previous password field.

To preserve that password field during postback, we can write  in code behind file like this

txtpassword.Attributes[“value”] = txtpassword.Text;

Here Default.aspx is like this

<%@ Page Language=”C#” AutoEventWireup=”true”  CodeFile=”Default.aspx.cs” Inherits=”_Default” %>

<!DOCTYPE html PUBLIC “-//W3C//DTD XHTML 1.0 Transitional//EN” “”&gt;

<html xmlns=””&gt;
<head runat=”server”>

<form id=”form1″ runat=”server”>

<asp:Label ID=”lblPassword” runat=”server”  Text=”Password” /> &nbsp&nbsp;
<asp:TextBox ID=”txtpassword” TextMode=”Password” runat=”server”></asp:TextBox>

<br />

<br />

<asp:Label ID=”lblCountry” runat=”server” Text=”Country ” /> &nbsp&nbsp; &nbsp;
<asp:DropDownList ID=”DropCountry” runat=”server” AutoPostBack=”True”
Height=”16px” Width=”130px”

<br />
<br />
<asp:Button ID=”BtnSubmit” runat=”server” Text=”Submit”
onclick=”BtnSubmit_Click” />


Code behind file is like this

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;

public partial class _Default : System.Web.UI.Page
protected void Page_Load(object sender, EventArgs e)

protected void BtnSubmit_Click(object sender, EventArgs e)

protected void DropCountry_SelectedIndexChanged(object sender, EventArgs e)

txtpassword.Attributes[“value”] = txtpassword.Text;

//Code for other operation



Leave a Reply

Fill in your details below or click an icon to log in: Logo

You are commenting using your account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s

This site uses Akismet to reduce spam. Learn how your comment data is processed.