日期:2014-05-18  浏览次数:20462 次

请帮忙看看,下面的代码为什么总也设置不了Cookies
登录页面,我想把用户名保存在Cookies中,下次自动取出,不知为什么实现不了?
代码如下:
private   void   Page_Load(object   sender,   System.EventArgs   e)
{
//   在此处放置用户代码以初始化页面
txtUserName.Text=(Request.Cookies[ "UserName1 "]!=null)   ?   Request.Cookies[ "UserName1 "].Value.ToString(): " ";  
}


private   void   Button1_Click(object   sender,   System.EventArgs   e)
{
Response.Cookies[ "UserName1 "].Value=txtUserName.Text;
Response.Cookies[ "UserName1 "].Expires=new   DateTime(2010,1,1);
Response.Cookies.Add(Response.Cookies[ "UserName1 "]);
Server.Transfer( "VerifySignIn.aspx ");
}

------解决方案--------------------
贴一段我的代码,参考下
protected void Page_Load(object sender, EventArgs e)
{
if (!IsPostBack)
{
if (Request.Cookies[ "user_name "] != null)
{
if (Request.Cookies[ "user_name "][ "uid "] != null)
{
this.txt_uid.Text = Request.Cookies[ "user_name "][ "uid "].ToString();
}
}
}
this.TextBox1.Text = this.Request.QueryString[ "pageid "].ToString();
}

protected void btn_login_Click(object sender, EventArgs e)
{
if (cb_confirm.Checked)
{
if ((Request.Cookies[ "user_name "] != null) && (Request.Cookies[ "user_name "][ "uid "] != null))
{
if ((Request.Cookies[ "user_name "][ "uid "].ToString() != this.txt_uid.Text))
{
SetCookie();
}
}
else
{
SetCookie();
}

}
Response.Write( " <script language= 'javascript '> alert( '登录成功 ') </script> ");
}

private void SetCookie()
{
HttpCookie cookie_name = new HttpCookie( "user_name ");
cookie_name[ "uid "] = this.txt_uid.Text;
cookie_name.Expires = DateTime.Now.AddDays(1d);
Response.AppendCookie(cookie_name);
}
------解决方案--------------------
HttpCookie cookie_name = new HttpCookie( "user_name ");
cookie_name[ "name "].values = this.txt_uid.Text;