• Cookie的使用


    using System;
    using System.Collections.Generic;
    using System.Linq;
    using System.Web;
    using System.Web.UI;
    using System.Web.UI.WebControls;
    
    public partial class Cookie : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {
    
        }
    
        //创建Cookie
        protected void Button1_Click(object sender, EventArgs e)
        {
            HttpCookie Cookie = new HttpCookie("MyCookie");
            DateTime dt=DateTime.Now;
            TimeSpan ts=new TimeSpan (0,0,1,0,0);//过期时间设置为1分钟
    
            //设置过期时间
            Cookie.Expires = dt.Add(ts);
            Cookie.Values.Add("name","yys");
            Cookie.Values.Add("age","24");
    
            Response.AppendCookie(Cookie);
            
        }
        //读取
        protected void Button2_Click(object sender, EventArgs e)
        {
            if(Request.Cookies["Mycookie"]!=null)
            {
                Response.Write(Request.Cookies["Mycookie"].Value);                    
            }
        }
    
        //修改
        protected void Button3_Click(object sender, EventArgs e)
        {
            //获取客户端的Cookie对象
            HttpCookie cok = Request.Cookies["MyCookie"];
    
            if(cok!=null)
            {
                cok.Values["name"] = "Abner";
                cok.Values.Set("age","20");
    
                //往Cookie里加入新的内容
                cok.Values.Set("sex","男");
    
                Response.AppendCookie(cok);
    
            
                Response.Write(Request.Cookies["MyCookie"].Value);
                
            }
    
        }
        //删除
        protected void Button4_Click(object sender, EventArgs e)
        {
            HttpCookie co=Request.Cookies["MyCookie"];
    
            co.Values.Remove("sex");//删除键为“sex”的键值对
            Response.Write(Request.Cookies["MyCookie"].Value);
    
            TimeSpan ts = new TimeSpan(-1,0,0,0);
            co.Expires = DateTime.Now.Add(ts);//删除整个Cookie,只要把过期时间设置为现在
            
        }
    }
    

      

  • 相关阅读:
    expdp定时备份
    设计模式简介
    利用Python制作万年历
    Linux下Python的安装
    排序算法-直接插入排序
    排序算法-冒泡排序
    数据结构-循环顺序队列&链队列
    数据结构-栈&链栈
    数据结构-双向链表&双向循环链表
    数据结构-单链表&单循环链表
  • 原文地址:https://www.cnblogs.com/YanYongSong/p/4409243.html
Copyright © 2020-2023  润新知