zoukankan      html  css  js  c++  java
  • 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
    }
  • 相关阅读:
    hadoop之 hadoop日志存放路径
    grpc的数据包监控
    HTTP2 概述
    gRPC的简单Go例子
    win下环境变量的设置
    Go的pprof使用
    graphviz
    学习Golang的步骤建议
    golang 的 sync.WaitGroup
    【转】golang的channel的几种用法
  • 原文地址:https://www.cnblogs.com/xyangs/p/2466760.html
Copyright © 2011-2022 走看看