1. Create the SessionMgr class
Using System;
Using System. Collections. Generic;
Using System. Linq;
Using System. Web;
/// <Summary>
/// Summary of SessionMgr
/// </Summary>
Public class SessionMgr
{
Private static IDictionary <string, IDictionary <string, object> data = new Dictionary <string, IDictionary <string, object> ();
Public static IDictionary <string, object> GetSession (string sessionId)
{
If (data. ContainsKey (sessionId ))
{
Return data [sessionId];
}
Else
{
IDictionary <string, object> session = new Dictionary <string, object> ();
Data [sessionId] = session;
Return session;
}
}
Public static IDictionary <string, object> GetSession ()
{
Throw new NotImplementedException ();
}
}
2. Read the server Session on the page
Using System;
Using System. Collections. Generic;
Using System. Linq;
Using System. Web;
Using System. Web. UI;
Using System. Web. UI. WebControls;
Public partial class sessionweb: System. Web. UI. Page
{
Protected void Page_Load (object sender, EventArgs e)
{
If (Request. Cookies ["MySessionId"] = null)
{
String sessionId = Guid. NewGuid (). ToString ();
Response. SetCookie (new HttpCookie ("MySessionId", sessionId ));
}
}
Protected void button#click (object sender, EventArgs e)
{
String sessionId = Request. Cookies ["MySessionId"]. Value;
IDictionary <string, object> session = SessionMgr. GetSession (sessionId );
Session ["server"] = "333 ";
}
Protected void Button2_Click (object sender, EventArgs e)
{
String sessionId = Request. Cookies ["MySessionId"]. Value;
IDictionary <string, object> session = SessionMgr. GetSession (sessionId );
Button2. Text = Convert. ToString (session ["server"]);
}
}
From programmers