日期: 2023.10.19 星期二
方法 3. SESSION
像是一個全域變數,放在 Server
,資料型態是物件。Session
的資料保存於伺服器端,但是預設只能保留 20 分鐘
,若 20 分鐘之內
,未再連線,伺服器將自動刪除該資料,避免一大堆 Session
資料,存放於伺服器端,癱瘓機器。
-
新增
ASP.NET
專案後,加入三表單WebForm1
、WebForm2
、WebForm3
-
WebForm1
:- 介面:
TextBox
1、Button
1 - 功能: 按下
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
上,按下按鈕時,跳轉到WebForm3
protected 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(); }
-
留言