Asp. NET remember to login user name specific implementation _ Practical skills

Source: Internet
Author: User

In the. aspx file

Copy Code code 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 username" font-size= "Small" forecolor= "Gray"/>
...

In the. aspx.cs file

Copy Code code as follows:

protected void Page_Load (object sender, EventArgs e)
{
if (! IsPostBack)
{
This.txtUser_Id.Focus ();
if (! Object.Equals (request.cookies["UserID"], NULL)
{
Create a cookie object that implements remembering user names
HttpCookie Readcookie = request.cookies["UserID"];
This.txtUser_Id.Text = Readcookie. Value;
}
}
}
private void Createcookie ()
{
Create a Cookie Object
HttpCookie cookie = new HttpCookie ("UserID");
To determine whether a CheckBox control is selected
if (this.cbxRemeberUser.Checked)
{
Store a user number in a created cookie object
Cookie. Value = This.txtUser_Id.Text;
}
Gets the expiration time of the created cookie object
Cookie. Expires = DateTime.MaxValue;
Add the created cookie object to the internal cookie collection
Response.appendcookie (cookie);
}

protected void Btninsert_click (object sender, ImageClickEventArgs e)
{
...
if (object. Equals (request.cookies["UserID"], NULL)
{
Invoke Custom Method Createcookie () store user name
Createcookie ();
}
Else
{
Createcookie ();
}
...
}

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.