Remember the implementation of the login username

  • 2020-06-15 08:00:19
  • OfStack

aspx file

 
 ... 
<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

 
    protected void Page_Load(object sender, EventArgs e)
    {
        if (!IsPostBack)
        {
            this.txtUser_Id.Focus();
            if (!Object.Equals(Request.Cookies["UserID"], null))
            {
                // create 1 a Cookie Object, the implementation remembers the user name 
                HttpCookie readcookie = Request.Cookies["UserID"];
                this.txtUser_Id.Text = readcookie.Value;
            }
        }
    }
    private void CreateCookie()
    {
        // create 1 a Cookie object 
        HttpCookie cookie = new HttpCookie("UserID");
        // judge Checkbox Control is selected 
        if (this.cbxRemeberUser.Checked)
        {
            // Store the user number to the created Cookie In the object 
            cookie.Value = this.txtUser_Id.Text;
        }
        // Get the created Cookie The expiration time of an object 
        cookie.Expires = DateTime.MaxValue;
        // Will create the Cookie Object is added to the interior Cookie In the collection 
        Response.AppendCookie(cookie);
}
    protected void btnInsert_Click(object sender, ImageClickEventArgs e)
    {
 ... 
        if (object.Equals(Request.Cookies["UserID"], null))
        {
          // Invoke the custom method  CreateCookie() Store user name 
          CreateCookie();
        }
        else
        {
           CreateCookie();
        }
 ... 
}


Related articles: