首页 > 代码库 > Cookie的设置与取值

Cookie的设置与取值

cookie是浏览器中专门存放“小块数据”的一快地方。 设置,和读取cookie,可以在服务端设置和读取,也可以在客户端设置和读取

WebForm1.aspx页面

<%@ Page Language="C#" AutoEventWireup="true" CodeBehind="WebForm1.aspx.cs" Inherits="WebApplication3.WebForm1" %>

<!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>
    <script src=http://www.mamicode.com/"JqueryCookies/jquery-1.11.1.js" type="text/javascript"></script>>


WebForm1.cs代码

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;

namespace WebApplication3
{
    public partial class WebForm1 : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {

        }

        protected void Button1_Click(object sender, EventArgs e)
        {
            Response.SetCookie(new HttpCookie("UserName", TextBox1.Text)); //在服务端 设置Cookie
        }

        protected void Button2_Click(object sender, EventArgs e)
        {
            TextBox2.Text= Request.Cookies["UserName"].Value;   //在服务端 获取Cookie
        }
    }
}