set.aspx代码如下:
<%@ Page Language="C#" AutoEventWireup="true" CodeFile="Sample_05.aspx.cs" Inherits="Sample_05" %>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
<title></title>
<link href="StyleSheet.css" rel="stylesheet" type="text/css" />
</head>
<form id="form1" runat="server">
<h3>Cookie 应用</h3>
<p> <asp:Button ID="Button1" runat="server" Text="设置一个单值cookie:(animal = 猫)"
οnclick="Button1_Click" />
<p> <asp:Button ID="Button3" runat="server" Text="清除单值的那个cookie:(animal = 猫)"
οnclick="Button3_Click" />
<p> <asp:Button ID="Button2" runat="server" Text="设置一个多值的cookie:(性别=男,姓名=小王,年龄=18)"
οnclick="Button2_Click" />
<p> <asp:Button ID="btn_mck_del" runat="server" Text="整个删除这个多值cookie" οnclick="btn_mck_del_Click"
<asp:Button ID="btn_ck_del_2" runat="server" Text="可以实现删除的另一个语句"
οnclick="btn_ck_del_2_Click" />
<asp:TextBox ID="txt_name" runat="server"></asp:TextBox>
<asp:Button ID="btn_edit" runat="server" Text="修改子键name的值"
οnclick="btn_edit_Click" />
<asp:Button ID="btn_delage" runat="server" Text="删除age子键" οnclick="btn_delage_Click"
<asp:Button ID="btn_delall" runat="server" Text="删除所有的子键"
οnclick="btn_delall_Click" />
<span class="spMemo">通过交互设置Cookie:</span><br /><br />
选择希望文字的颜色:
<asp:DropDownList ID="ddl_color" runat="server">
<asp:ListItem Value="red">红色</asp:ListItem>
<asp:ListItem Value="green">绿色</asp:ListItem>
<asp:ListItem Value="blue">蓝色</asp:ListItem>
</asp:DropDownList>
<br /><br />
选择希望文字的大小:
<asp:DropDownList ID="ddl_fontSize" runat="server">
<asp:ListItem Value="12px">12</asp:ListItem>
<asp:ListItem Value="14px">14</asp:ListItem>
<asp:ListItem Value="18px">18</asp:ListItem>
<asp:ListItem Value="25px">25</asp:ListItem>
</asp:DropDownList>
<br /><br />
<asp:Button ID="btn_setColor" runat="server" Text="设置颜色喜好"
οnclick="btn_setColor_Click" />
public partial class Sample_05 : System.Web.UI.Page
protected void Page_Load(object sender, EventArgs e)
protected void Button1_Click(object sender, EventArgs e)
//HttpCookie ck=new HttpCookie("animal","猫") ; //单值COOKIE可以将主键和值写在一起
//声明创建
HttpCookie ck_animal=new HttpCookie("animal");
//设置相关值
ck_animal.Value = "猫";
//设置过期时间
ck_animal.Expires = DateTime.Now.AddDays(1);
//写入客户端
Response.Cookies.Add(ck_animal);
protected void Button3_Click(object sender, EventArgs e)
//Response.Cookies["animal"].Expires = DateTime.Now.AddDays(-1);
//这种写法最标准
Response.Cookies["animal"].Expires = DateTime.MinValue;
protected void Button4_Click(object sender, EventArgs e)
foreach (string _key in Request.Cookies.AllKeys) {
Response.Cookies[_key].Expires = DateTime.MinValue;
protected void btn_setColor_Click(object sender, EventArgs e)
HttpCookie ck_CSS = new HttpCookie("CSS"); //声明创建
string _color = ddl_color.SelectedValue; //获取值
string _size = ddl_fontSize.SelectedValue;
ck_CSS.Values.Add("color", _color); //设置相关值
ck_CSS.Values.Add("fontsize", _size);
ck_CSS.Expires = DateTime.Now.AddMonths(1);
Response.Cookies.Add(ck_CSS); //写入
Response.Cookies["multi_cookie"].Expires = DateTime.MinValue;
protected void btn_ck_del_2_Click(object sender, EventArgs e)
//请结合ASP.NET生命周期,和COOKIE的运行模式,思考这个语句
//此方法非标准用法,可以看做是COOKIE应用逻辑上的一个漏洞,或许哪天就不起作用了
HttpCookie ck = Response.Cookies["multi_cookie"];
protected void btn_edit_Click(object sender, EventArgs e)
string _name = txt_name.Text;
if (_name != "") {
HttpCookie ck = Request.Cookies["multi_cookie"];
ck.Values.Set("name", _name);
ck.Expires = DateTime.MaxValue;
Response.Cookies.Add(ck);
protected void btn_delage_Click(object sender, EventArgs e)
HttpCookie ck = Request.Cookies["multi_cookie"];
ck.Values.Remove("age");
ck.Expires = DateTime.MaxValue;
Response.Cookies.Add(ck);
protected void btn_delall_Click(object sender, EventArgs e)
HttpCookie ck = Request.Cookies["multi_cookie"];
ck.Values.Clear();
ck.Expires = DateTime.MaxValue;
Response.Cookies.Add(ck);
read.aspx代码如下:
<%@ Page Language="C#" AutoEventWireup="true" CodeFile="Sample_05_readcookie.aspx.cs" Inherits="Sample_05_readcookie" %>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
<title></title>
<link href="StyleSheet.css" rel="stylesheet" type="text/css" />
</head>
<form id="form1" runat="server">
<h3>Cookies应用</h3>
<p> <span class="spMemo">animal的值:</span><asp:TextBox ID="txt_animal" runat="server"></asp:TextBox>
<p> <span class="spMemo">多值Cookie读取:</span>
<asp:TextBox ID="txt_multiCookie" runat="server"></asp:TextBox>
<p> <span class="spMemo" >交互信息读取应用:</span>
<asp:Label ID="Lab_CSS" runat="server" Text="Label"></asp:Label>
<asp:Button ID="Button1" runat="server" οnclick="Button1_Click"
Text="读取cookie_CSS" />
<p> <span class="spMemo" >通过遍历连续一并输出cookie的内容:</span>
<asp:Button ID="Button2" runat="server" Text="遍历所有cookie"
οnclick="Button2_Click" /><br /><br />
<asp:TextBox ID="txt_allkey" runat="server" TextMode="MultiLine" Rows="10" Columns="50"></asp:TextBox>
<p class ="divTc"> <a href="Sample_05.aspx">返回设置页面</a>
</form>
</body>
</html>
read.aspx.cs代码如下:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
public partial class Sample_05_readcookie : System.Web.UI.Page
protected void Page_Load(object sender, EventArgs e)
//读取单值Cookie:animal
if (Request.Cookies["animal"] != null) {
txt_animal.Text = Request.Cookies["animal"].Value;
//读取多值Cookie: multi_cookie [name,sex,age]
if (Request.Cookies["multi_cookie"] != null)
//这样写语句太长太繁琐,需要适当使用临时的对象参与编程
txt_multiCookie.Text += Request.Cookies["multi_cookie"].Values["name"] + ",";
txt_multiCookie.Text += Request.Cookies["multi_cookie"].Values["sex"] + ",";
txt_multiCookie.Text += Request.Cookies["multi_cookie"].Values["age"] + "岁";
//这样的程序语句更一目了然,更易维护
//必须确定主键名称 和 子键名称
HttpCookie _ck = Request.Cookies["multi_cookie"];
txt_multiCookie.Text = _ck.Values["name"] + ","
+ _ck.Values["sex"] + ","
+ _ck.Values["age"] + "岁";
protected void Button1_Click(object sender, EventArgs e)
if (Request.Cookies["CSS"] != null) {
HttpCookie ck_css = Request.Cookies["CSS"];
string _color = ck_css.Values["color"];
string _fontsize = ck_css.Values["fontsize"];
Lab_CSS.Style.Add("color", _color);
Lab_CSS.Style.Add("font-size", _fontsize);
protected void Button2_Click(object sender, EventArgs e)
foreach ( string _key in Request.Cookies.AllKeys ) {
HttpCookie _ck = Request.Cookies[_key];
foreach (string _subkey in _ck.Values.Keys) {
txt_allkey.Text += _key + "|" + _subkey + ":" + _ck.Values[_subkey] + "\n";
界面如下:
下面实例:set.aspx代码如下: 跳转 <asp:Button ID="Button3" runat="server" onclick="Button3_Click" style="height: 21px" Text=" 设置一个多值 cookie"
1.多值Cookie的添加
HttpCookie ck2=new HttpCookie("multi_cookie"); //声明,创建
ck2.Values.Add("name","小王");
ck2.Values.Add("ag
一般的前台和后台交互传值很简单,我习惯了用Ajax,但是最近遇到一个问题,前端调用后台一个方法后,需要刷新整个页面,但是这时候前台和后台方法都没有走完,之前利用JQuery获取的那些值都清空了,于是就想到了Cookie,因为Cookie可以保存在浏览器中,我们可以设置它的有效时间。以下是小编的代码。
function queryListByMonth() {
什么是cookies
Cookies有很多种,网站上的cookie,浏览器的cookie,会话cookie等,cookies是存储浏览器与网站访问时的数据,是Web服务器与客户端连接的方式之一。用户访问不同的站点,各个站点会向用户的浏览器发送一个cookie,浏览器会分别存储。实际上,cookies是一小段文本文件,发生在用户请求和页面在Web服务器与浏览器之间传递。
Cooki...
本文出自:http://www.cnblogs.com/xiaoshuai/default.html?page=3
HttpCookie cookie = new HttpCookie["aspcn"];
cookie.Value = "飞刀";
Response.AppendCookie(cookie);
我们取出Cookie值也很简单
HttpCookie cookie
Android错误之(Android 6.0)Unable to add window android.view.ViewRootImpl$W@d4521e8 -- permission denied
30653