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”>
<html xmlns=”http://www.w3.org/1999/xhtml”>
<head runat=”server”>
<title></title>
</head>
<body>
<form id=”form1″ runat=”server”>
<div>
<asp:Label ID=”lblPassword” runat=”server” Text=”Password” />  
<asp:TextBox ID=”txtpassword” TextMode=”Password” runat=”server”></asp:TextBox>
<br />
<br />
<asp:Label ID=”lblCountry” runat=”server” Text=”Country ” />  
<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
}
}