asp.net頁間傳值之多值cookies
阿新 • • 發佈:2019-02-12
下面例項:
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> <body> <form id="form1" runat="server"> <div> <h3>Cookie 應用</h3> <p> <asp:Button ID="Button1" runat="server" Text="設定一個單值cookie:(animal = 貓)" onclick="Button1_Click" /> </p> <p> <asp:Button ID="Button3" runat="server" Text="清除單值的那個cookie:(animal = 貓)" onclick="Button3_Click" /> </p> <hr /> <p> <asp:Button ID="Button2" runat="server" Text="設定一個多值的cookie:(性別=男,姓名=小王,年齡=18)" onclick="Button2_Click" /> </p> <p> <asp:Button ID="btn_mck_del" runat="server" Text="整個刪除這個多值cookie" onclick="btn_mck_del_Click" /> </p> <p> <asp:Button ID="btn_ck_del_2" runat="server" Text="可以實現刪除的另一個語句" onclick="btn_ck_del_2_Click" /> </p> <p> <asp:TextBox ID="txt_name" runat="server"></asp:TextBox> <asp:Button ID="btn_edit" runat="server" Text="修改子鍵name的值" onclick="btn_edit_Click" /> </p> <p> <asp:Button ID="btn_delage" runat="server" Text="刪除age子鍵" onclick="btn_delage_Click" /> </p> <p> <asp:Button ID="btn_delall" runat="server" Text="刪除所有的子鍵" onclick="btn_delall_Click" /> </p> <p> <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="設定顏色喜好" onclick="btn_setColor_Click" /> </p> <p> <asp:Button ID="Button4" runat="server" Text="清除所有的cookie" onclick="Button4_Click" /> </p> <p class ="divTc "><a href="Sample_05_readcookie.aspx" >跳轉到讀取cookie的頁面</a> </p> </div> </form> </body> </html>
set.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 : 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); } //按鈕新增一個多值的Cookie protected void Button2_Click(object sender, EventArgs e) { //宣告建立 HttpCookie ck2 = new HttpCookie("multi_cookie"); //設定相關值 ck2.Values.Add("name", "小王"); ck2.Values.Add("age", "18"); ck2.Values.Add("sex", "男"); //設定過期時間:永不過期 ck2.Expires = DateTime.MaxValue; //寫入客戶端 Response.Cookies.Add(ck2); } //清除單值的Cookie protected void Button3_Click(object sender, EventArgs e) { //Response.Cookies["animal"].Expires = DateTime.Now.AddDays(-1); //這種寫法最標準 Response.Cookies["animal"].Expires = DateTime.MinValue; } //遍歷清除所有的Cookie 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); //寫入 } protected void btn_mck_del_Click(object sender, EventArgs e) { //標準的刪除語句 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> <body> <form id="form1" runat="server"> <div> <h3>Cookies應用</h3> <p> <span class="spMemo">animal的值:</span><asp:TextBox ID="txt_animal" runat="server"></asp:TextBox> </p> <p> <span class="spMemo">多值Cookie讀取:</span> <asp:TextBox ID="txt_multiCookie" runat="server"></asp:TextBox> </p> <p> <span class="spMemo" >互動資訊讀取應用:</span> <asp:Label ID="Lab_CSS" runat="server" Text="Label"></asp:Label> <asp:Button ID="Button1" runat="server" onclick="Button1_Click" Text="讀取cookie_CSS" /> </p> <p> <span class="spMemo" >通過遍歷連續一併輸出cookie的內容:</span> <asp:Button ID="Button2" runat="server" Text="遍歷所有cookie" onclick="Button2_Click" /><br /><br /> <asp:TextBox ID="txt_allkey" runat="server" TextMode="MultiLine" Rows="10" Columns="50"></asp:TextBox> </p> <p class ="divTc"> <a href="Sample_05.aspx">返回設定頁面</a> </p> </div> </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";
}
}
}
}
介面如下: