014. asp.net实现记住密码的功能

  protected void Button1_Click(object sender, EventArgs e)
{
if (txtname.Text.Trim().Equals("mr") && txtpwd.Text.Trim().Equals("mrsoft"))
{
Session["username"] = txtname.Text.Trim();
if (ckbauto.Checked)
{
if (Request.Cookies["username"] == null)
{
Response.Cookies["username"].Expires = DateTime.Now.AddDays();
Response.Cookies["userpwd"].Expires = DateTime.Now.AddDays();
Response.Cookies["username"].Value = txtname.Text.Trim();
Response.Cookies["userpwd"].Value = txtpwd.Text.Trim();
}
}
Response.Redirect("admin.aspx");
}
else
{
ClientScript.RegisterStartupScript(this.GetType(),"","alert('用户名或密码错误!');",true);
}
}
protected void txtname_TextChanged(object sender, EventArgs e)
{
if (Request.Cookies["username"] != null)
{
if (Request.Cookies["username"].Value.Equals(txtname.Text.Trim()))
{
txtpwd.Attributes["value"] = Request.Cookies["userpwd"].Value;
}
}
}

注: 对于txtname控件, 需要将其的AutoPostBack属性, 设置为True, 这样当用户输入用户名之后, 会自动回发到服务器, 执行TextChanged事件

上一篇:趣拍SDK接入问题Android


下一篇:Y2错题解析