当前位置:编程学习 > C#/ASP.NET >>

关于cookie创建的问题

怎样从无到有创建一个cookie,并给它赋值 --------------------编程问答-------------------- Response.Cookies.Add(new   HttpCookie("名称",   值)) --------------------编程问答-------------------- 楼上正解 --------------------编程问答-------------------- 另:COOKIES操作时请注意.每次都最好先remove,再加,不然有时会引起存二个cookie --------------------编程问答--------------------

public partial class Cookies : System.Web.UI.Page
{
    protected void Page_Load(object sender, EventArgs e)
    {
        
        
    }
    protected void Button1_Click(object sender, EventArgs e)
    {
     //清除cookies
       // Request.Cookies..clear();
        //将cookies["username"]置空
        HttpCookie usercookie = Request.Cookies["username"];
       
       usercookie.Expires = System.DateTime.Now.AddDays(-10);
       // usercookie.Expires=.AddDays(-1.0);
        Response.Cookies.Add(usercookie);
    }

    protected void Button2_Click(object sender, EventArgs e)
    {
//读取cookies
        HttpCookie mycookie = Request.Cookies["username"];
       
        Response.Write(mycookie.Value);
    }
    protected void Button3_Click(object sender, EventArgs e)
    {
//写入cookies
        HttpCookie usercookie = new HttpCookie("username", "test");
        //usercookie.Expires = System.DateTime.Now.AddMinutes(1);
        usercookie.Expires = System.DateTime.Now.AddDays(1);
        Response.Cookies.Add(usercookie);
    }
}


补充:.NET技术 ,  ASP.NET
CopyRight © 2022 站长资源库 编程知识问答 zzzyk.com All Rights Reserved
部分文章来自网络,