注册 登录
编程论坛 ASP.NET技术论坛

写入Cookie

陌生人2007 发布于 2007-11-29 20:56, 1381 次点击
protected void Button4_Click(object sender, EventArgs e)
    {
        //写入Cookie
        HttpCookie nc = new HttpCookie("newCookie");
        nc.Value["name"] = "HMS";
        nc.Value["age"] = "25";
        nc.Value["dt"] = DateTime.Now.ToString();
        Response.Cookies.Add(nc);
        Response.Write("Cookie写入成功!");
    }

不知道这段代码错在哪了,下面是错误提示:
错误 1 与“string.this[int]”最匹配的重载方法具有一些无效参数 D:\WebAppliation\WebAppliationtest\Default.aspx.cs 42 9 D:\WebAppliation\WebAppliationtest\
错误 2 参数“1”: 无法从“string”转换为“int” D:\WebAppliation\WebAppliationtest\Default.aspx.cs 42 18 D:\WebAppliation\WebAppliationtest\
总共6处,不过是重复的,nc三处都错另外三处是"name"、"age"、"dt"
3 回复
#2
explorer_zh2007-11-30 10:00
这里nc.Value[int]不能为string类型
HttpCookie nc = new HttpCookie("newCookie"),newCookie是Cookie名称
nc.Value是值
如果你想添加多个Cookie的话
应如下:
HttpCookie nc 1= new HttpCookie("name");
HttpCookie nc2 = new HttpCookie("age");
HttpCookie nc 3= new HttpCookie("dt");
nc1.Value="HMS";
nc2.Value= "25";
nc3.Value=DateTime.Now.ToString();
Response.Cookies.Add(nc1);
Response.Cookies.Add(nc2);
Response.Cookies.Add(nc3);
 Response.Write("Cookie写入成功!");
#3
洁洁2007-11-30 10:32
写cookie原来这么麻烦啊!我以前都不是这么写的。
Response.Cookies["name"].Value = "HMS";这样就写好了
#4
风之灵2013-05-23 16:37
nc.Value["name"] = "HMS";
        nc.Value["age"] = "25";
        nc.Value["dt"] = DateTime.Now.ToString();
 你的这一段里面的value改成values 就可以了
1