C#后台操作Cookie给密码框赋值的问题

来源:互联网 发布:java 构造方法 public 编辑:程序博客网 时间:2024/05/17 07:43
**

C#后台操作Cookie给密码框赋值的问题

**
今天碰到了这么一个状况:一个挺简单的记住密码功能。使用Cookie存值,登录时从cookie里取值给文本框。但是用户名可以成功赋值,密码框类型为password却赋值不上。使用以下方法解决:
写Cookie : HttpCookie Ucookie = new HttpCookie("USER_COOKIE");
if (this.Csave.Checked == true)
{
Ucookie.Values.Add("Name", txtUserName.Value);
Ucookie.Values.Add("Pwd", txtPwd.Value);
Ucookie.Expires = System.DateTime.Now.AddDays(7.0);
HttpContext.Current.Response.Cookies.Add(Ucookie);
}
else
{
if (Response.Cookies["USER_COOKIE"] != null)
Response.Cookies["USER_COOKIE"].Expires = DateTime.Now;
}

读Cookie :HttpCookie cookies = Request.Cookies["USER_COOKIE"];
if (cookies != null && cookies.HasKeys)
{
//用户名赋值
txtUserName.Value = cookies["Name"];
// 密码框赋值
//txtPwd.Attributes.Add("value", cookies.Values["Pwd"].ToString()); 此方法赋不上值!
//Page.RegisterStartupScript("", "<script>document.getElementById('txtPwd').value=" + cookies["Pwd"].ToString() + ";</script>"); 此方法可能出现类型不对的状况,比如000000 会赋值为0
Page.RegisterStartupScript("", "<script>document.getElementById('txtPwd').value=" + "'" + cookies["Pwd"].ToString() + "'" + ";</script>");
// 设置勾选记住密码
this.Csave.Checked = true;
}

0 0