日期: 2023.10.19 星期二
方法 3. SESSION
像是一個全域變數,放在 Server,資料型態是物件。Session 的資料保存於伺服器端,但是預設只能保留 20 分鐘,若 20 分鐘之內,未再連線,伺服器將自動刪除該資料,避免一大堆 Session 資料,存放於伺服器端,癱瘓機器。
-
新增
ASP.NET專案後,加入三表單WebForm1、WebForm2、WebForm3-
WebForm1:- 介面:
TextBox1、Button1 - 功能: 按下
Button,傳出TextBox1.Text的值,給WebForm2 -
程式:
protected void Button1_Click(object sender, EventArgs e){ //使用 Query String 方法 //Response.Redirect("WebForm2.aspx?myID=" + TetBox1.Text ); Session["myID"] = TextBox1.Text; Response.Redirect("WebForm2.aspx");
- 介面:
-
WebForm2- 介面:
Label* 1 - 功能: 顯示
WebForm1.aspx.cs傳送過來的值,並顯示 -
程式:
“`C#
protected void Page_Load(object sender, EventArgs e){
//使用 Query String 方法
//string aaa = Request.QueryString["myID"]);
//Label1.Tet = aaa;
Label1.Text = Session["myID"].ToString();
}
- 介面:
-
-
如何知到哪一個
Session是哪一個WebForm的Connection。
Session有一屬性叫做Session ID,每一次使用者提出一Request要求伺服器去執行WebForm,此刻瀏覽器會傳一個Sessin ID給伺服器,伺服器會記下此ID,當該WebForm執行完畢,它會把這Session ID預設方法透過Cookie把Session ID下載下來給你,所以下一次上傳時又把同一ID丟上去,所以伺服器就會知道是哪一個Session ID跟我做連線,實驗
- 在
WebForm2上多增加一Label2,用來顯示Session["ID"]的值 。 - 在
WebForm2上增加一Button1,用來連結到WebForm3。 - 在
WebForm3上建立Label * 2(WebForm1上的資料與其SessionID) -
在
WebForm2上,Label2.Text顯示SessionID(透過瀏覽器送來的SeeionID)“`C#
protected void Page_Load(object sender, EventArgs e){
//使用 Query String 方法
//string aaa = Request.QueryString["myID"]);
//Label1.Tet = aaa;
Label1.Text = Session["myID"].ToString();
//增加
Label2.Text = Session.SessionID;
} -
在
WebForm2上,按下按鈕時,跳轉到WebForm3protected void Button1_Click(object sender, EventArgs e){ Response.Redirect("WebForm3.aspx"); -
在
WebForm3於Page_Load事件時,於Label1.Text顯示WebForm1傳的值“`C#
protected void Page_Load(object sender, EventArgs e){
Label1.Text = Session["myID"].ToString();
//在顯示一次 SessionID
Label2.Text = Session.SessionID;
} -
測試
-
執行
WebForm1,TextBox1輸入abc,按下按鈕。
結果:abc sdasdsasdsaafasfasfasfa # SessionID -
按下現在畫面的
WebForm2按鈕,連結到WebForm3
結果:abc sdasdsasdsaafasfasfasfa # SessionID結論
同樣的SessionID,若不再使用這網站則該SessionID將會自動被刪除。
-
-
SesionID預設使用Cookie技術來做傳遞,但若關閉了Cookie就無法使用。
所以可使用URI類似Query String的方式。-
專案中找到
Web.config,修改為不用Cookie方式傳遞,並且每個Session的值,只保留 1 分鐘:: <system.web> <sessionState cookieless="UseUri" timeout="1"></sessionState> </system.web> : - 在開啟
WebForm1,網址會出現訊息:localhost:50817/(S(xxxxxxxxxxxx))/WebForm1.aspx
(S(xxxxxxxxxxxx))裡的xxxxxxxxxxx,即SessionID,透過URI的方式,上傳給Server,此刻按下WebForm1的按鈕,就會也看到Label1上顯示相同的SessionID
-
- 先移除專案中所修改
Web.config的部分
- 在
方法 4. Application
Application 與 Session 使用方法相同,差異在於任何一個網頁連結到的 Application 的物件是同一個。
所以沒有所謂不同的 SessioID 問題,大家連線進來都是同一物件,該物件產生時機是 IIS 啟動時,關閉 IIS 就消失了。
- 應用: 設計一個計數器,看有多少了逛過該網頁。
-
步驟:
-
增加一
WebForm4:- 介面:
Label1 * 1存放次數、Button * 1執行PostBack - 功能: 按下
Button,傳出TextBox1.Text的值,給WebForm2
- 介面:
-
若用
Session方法來做
程式:protected void Button1_Click(object sender, EventArgs e){ int counter; if (Session["count"] == null) counter =1; else{ counter = (int)Session["counter"] + 1; } Session["count"] = counter; Label1.Text = counter.ToString(); }執行
WebForm4後,按下Button1,Label1.Text的值會自動加一,沒有問題(假設為10)。
但模擬另一個使用者打開瀏覽器,連接這網址,按下Button會發現是從1開始,原因是使用了Session技術,每一個使用者連到那個伺服器的SessioID都不同所造成。所以不能用SESSION技術。 -
改成
Application物件的方法protected void Button1_Click(object sender, EventArgs e){ int counter; if (Application["count"] == null) counter =1; else{ counter = (int)Application["counter"] + 1; } Application["count"] = counter; Label1.Text = counter.ToString(); } -
使用
Application物件時,須考量到不同使用者操作若同時按下時,資料可能會亂掉。
建議修改前作Lock動作 (Application.Lock()),不要讓其他使用者同一時間作修改,protected void Button1_Click(object sender, EventArgs e){ int counter; Application.Lock(); if (Application["count"] == null) counter =1; else{ counter = (int)Application["counter"] + 1; } Application["count"] = counter; Application.UnLock(); Label1.Text = counter.ToString(); }
-
留言