Cookie怎么在两个页面间传递数据?

来源:百度知道 编辑:UC知道 时间:2024/09/23 11:55:36
Cookie到底怎么在两个页面间传递数据?
我在第一个页面写:
protected void Button1_Click(object sender, EventArgs e)
{
HttpCookie temp = new HttpCookie("newcookie");
temp.Values["name"] = "myname";
temp.Values["password"] = "123";
temp.Expires = DateTime.Now.AddMinutes(2);
Response.Cookies.Add(temp);
Response.Redirect("Default2.aspx");

}
然后在Default2.aspx.cs页面中写:
protected void Page_Load(object sender, EventArgs e)
{
HttpCookie temp = Response.Cookies["newcookie"];
Response.Write("<br>name:" + temp.Values["name"]);
Response.Write("<br>password:" + temp.Values["password"]);
}
为什么点按钮后跳到第二个页面后name和password值没有传递过来?
(跟Sessions不一样吗?)到底要怎么用Cookie?

参考代码如下:

//如果请求的Cookie对象为空
if (Request.Cookies["userCookie"] == null)
{
//创建一个Cookie对象
HttpCookie userCookie = new HttpCookie("userCookie");

//给对象赋值
userCookie.Values["userName"] =
userInfo.UserName.ToString();

userCookie.Values["lastVist"] =
DateTime.Now.ToString();

userCookie.Values["count"] = "1";

//设置对象的过期时间
userCookie.Expires = DateTime.Now.AddDays(30);

//添加到Cookies集合
Response.Cookies.Add(userCookie);
}
//不为空的情况
else
{
int coun