protected void Page_Load(object sender, EventArgs e)
{
if (!IsPostBack)
{
this.txtUser_Id.Focus();
if (!Object.Equals(Request.Cookies["UserID"], null))
{
//創(chuàng)建一個(gè)Cookie對(duì)象,實(shí)現(xiàn)記住用戶名
HttpCookie readcookie = Request.Cookies["UserID"];
this.txtUser_Id.Text = readcookie.Value;
}
}
}
private void CreateCookie()
{
//創(chuàng)建一個(gè)Cookie對(duì)象
HttpCookie cookie = new HttpCookie("UserID");
//判斷Checkbox控件是否被選中
if (this.cbxRemeberUser.Checked)
{
//將用戶編號(hào)存儲(chǔ)到創(chuàng)建的Cookie對(duì)象中
cookie.Value = this.txtUser_Id.Text;
}
//獲取創(chuàng)建的Cookie對(duì)象的過期時(shí)間
cookie.Expires = DateTime.MaxValue;
//將創(chuàng)建的Cookie對(duì)象添加到內(nèi)部Cookie集合中
Response.AppendCookie(cookie);
}
protected void btnInsert_Click(object sender, ImageClickEventArgs e)
{
…
if (object.Equals(Request.Cookies["UserID"], null))
{
//調(diào)用自定義方法 CreateCookie()存儲(chǔ)用戶名
CreateCookie();
}
else
{
CreateCookie();
}
…
}