• Cookie基本使用


    1 通过HTTPCookies类进行创建

    创建Cookies:

    HttpCookie StudentCookies = new HttpCookie("StudentCookies");
    StudentCookies.Value = TextBox1.Text;
    StudentCookies.Expires = DateTime.Now.AddHours(1);
    Response.Cookies.Add(StudentCookies);

    读取Cookies:

    string roll = Request.Cookies["StudentCookies"].Value;

    2直接通过Response进行创建

    创建Cookies:

    Response.Cookies["StudentCookies"].Value = TextBox1.Text;
    Response.Cookies["StudentCookies"].Expires = DateTime.Now.AddDays(1);

    读取Cookies:

    string roll = Request.Cookies["StudentCookies"].Value;

    3多值的存储

    创建Cookies:

     
    Response.Cookies["StudentCookies"]["RollNumber"] = TextBox1.Text;
    Response.Cookies["StudentCookies"]["FirstName"] = "Abhimanyu";
    Response.Cookies["StudentCookies"]["MiddleName"] = "Kumar";
    Response.Cookies["StudentCookies"]["LastName"] = "Vatsa";
    Response.Cookies["StudentCookies"]["TotalMarks"] = "499";
    Response.Cookies["StudentCookies"].Expires = DateTime.Now.AddDays(1); 
     

    读取Cookies:

     
    string roll;
    roll = Request.Cookies["StudentCookies"]["RollNumber"];
    roll = roll + " " + Request.Cookies["StudentCookies"]["FirstName"];
    roll = roll + " " + Request.Cookies["StudentCookies"]["MiddleName"];
    roll = roll + " " + Request.Cookies["StudentCookies"]["LastName"];
    roll = roll + " " + Request.Cookies["StudentCookies"]["TotalMarks"];
    Label1.Text = roll; 

    4.删除Cookies的方法

    删除Cookies的实质是修改它的过期时间,代码如下:

    if (Request.Cookies["StudentCookies"] != null)
    {
        Response.Cookies["StudentCookies"].Expires = DateTime.Now.AddDays(-1);
        Response.Redirect("Result.aspx");  //to refresh the page
    }
  • 相关阅读:
    vuejs 组件通讯
    导出pdf
    css 鼠标选中内容背景色
    console.log() 字体颜色
    使用cross-env解决跨平台设置NODE_ENV的问题
    Visual Studio动态生成版权信息(VS2015,VS2010,VS2008)
    程序员常用工具汇总
    存储过程分页
    oracle全表扫描
    CDM常用命令
  • 原文地址:https://www.cnblogs.com/xyangs/p/2466760.html
Copyright © 2020-2023  润新知