ASP. NET remembers the specific implementation of the Login User Name

Source: Internet
Author: User

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 ();
}
...
}

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.