In the. aspx File
Copy codeThe Code is as follows :...
<Asp: TextBox ID = "txtUser_Id" runat = "server" MaxLength = "4" Width = "120px" BorderColor = "LightSlateGray" BorderWidth = "1px"> </asp: textBox>
...
<Asp: ImageButton ID = "btnInsert" runat = "server" ImageUrl = "~ /Images/Login. GIF "OnClick =" btnInsert_Click "/>
...
<Asp: CheckBox ID = "cbxRemeberUser" runat = "server" Text = "Remember User Name" Font-Size = "Small" ForeColor = "gray"/>
...
. Aspx. cs File
Copy codeThe Code is as follows: protected void Page_Load (object sender, EventArgs e)
{
If (! IsPostBack)
{
This.txt User_Id.Focus ();
If (! Object. Equals (Request. Cookies ["UserID"], null ))
{
// Create a Cookie object to remember the user name
HttpCookie readcookie = Request. Cookies ["UserID"];
This.txt User_Id.Text = readcookie. Value;
}
}
}
Private void CreateCookie ()
{
// Create a Cookie object
HttpCookie cookie = new HttpCookie ("UserID ");
// Determine whether the Checkbox control is selected
If (this. cbxRemeberUser. Checked)
{
// Store the user ID in the Cookie object created
Cookie. Value = this.txt User_Id.Text;
}
// Obtain the expiration time of the created Cookie object
Cookie. Expires = DateTime. MaxValue;
// Add the created Cookie object to the internal Cookie set
Response. AppendCookie (cookie );
}
Protected void btnInsert_Click (object sender, ImageClickEventArgs e)
{
...
If (object. Equals (Request. Cookies ["UserID"], null ))
{
// Call the custom method CreateCookie () to store the user name
CreateCookie ();
}
Else
{
CreateCookie ();
}
...
}