How to preserve the password value of Textbox during postback ?


Hi

While creating the registration page in asp.net, 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” “http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd”&gt;

<html xmlns=”http://www.w3.org/1999/xhtml”&gt;
<head runat=”server”>
<title></title>
</head>
<body>

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

<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”
onselectedindexchanged=”DropCountry_SelectedIndexChanged”>
<asp:ListItem>Select</asp:ListItem>
<asp:ListItem>Nepal</asp:ListItem>
<asp:ListItem>India</asp:ListItem>
<asp:ListItem>US</asp:ListItem>
<asp:ListItem>UK</asp:ListItem>
</asp:DropDownList>

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

</div>
</form>
</body>
</html>

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

}
}

Advertisement

Leave a Reply

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

WordPress.com Logo

You are commenting using your WordPress.com 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.