标签: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="JqueryCookies/jquery-1.11.1.js" type="text/javascript"></script> <script src="JqueryCookies/jquery.cookie.js" type="text/javascript"></script> <script type="text/javascript"> 当页面刚加载就取得的Cookie为UserName的值, $(function () { $("#TextBox3").val($.cookie("UserName")); //在客户端 获取Cookie }) </script> </head> <body> <form id="form1" runat="server"> <%----------------------------------------------------------------------------%> <div> <asp:TextBox ID="TextBox1" runat="server"></asp:TextBox> <asp:Button ID="Button1" runat="server" OnClick="Button1_Click" Text="设置Cookie" /> </div> <%----------------------------------------------------------------------------%> <asp:TextBox ID="TextBox2" runat="server"></asp:TextBox> <asp:Button ID="Button2" runat="server" OnClick="Button2_Click" Text="获取Cookie" /> <%----------------------------------------------------------------------------%> <p> <asp:Label ID="Label1" runat="server" Text="在Html页用jquery取得Cookie值:"></asp:Label> <asp:TextBox ID="TextBox3" runat="server"></asp:TextBox> </p> <%----------------------------------------------------------------------------%> </form> </body> </html>
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 } } }
标签:cookie的设置与取值
原文地址:http://blog.csdn.net/fanbin168/article/details/38370651