無論如何,我決定以另一種方式做,我稱之爲SessionManager。我只是想在每次必須使用它時輸入Session變量名稱,這是爲了防止某些會話變量名稱錯誤。因此,這就是我所想到的。
using System;
using System.Collections;
using System.Collections.Generic;
using System.Web;
using System.Web.SessionState;
public class SessionManager : IRequiresSessionState
{
public SessionManager()
{
}
string _sessionID = string.Empty;
public string SessionID
{
get
{
if (Get("SessionID") == null) Save("SessionID", string.Empty);
return Get("SessionID").ToString();
}
set
{
this._sessionID = value;
Save("SessionID", value);
}
}
bool _isLoggedIn = false;
public bool isLoggedIn
{
get
{
if (Get("isLoggedIn") == null) Save("isLoggedIn", false);
return Convert.ToBoolean(Get("isLoggedIn").ToString());
}
set
{
this._isLoggedIn = value;
Save("isLoggedIn", value);
}
}
string _username = string.Empty;
public string Username
{
get
{
if (Get("Username") == null) Save("Username", string.Empty);
return Get("Username").ToString();
}
set
{
this._username = value;
Save("Username", value);
}
}
int _selectedCID = 0;
public int SelectedCID
{
get
{
if (Get("SelectedCID") == null) Save("SelectedCID", 0);
return Convert.ToInt32(Get("SelectedCID"));
}
set
{
this._selectedCID = value;
Save("SelectedCID", value);
}
}
string _selectedCIDValPath = string.Empty;
public string SelectedCIDValPath
{
get
{
if (Get("SelectedCIDValPath") == null) Save("SelectedCIDValPath", 0);
return Get("SelectedCIDValPath").ToString();
}
set
{
this._selectedCIDValPath = value;
Save("SelectedCIDValPath", value);
}
}
#region methods
void Save(string name, object value)
{
if (HttpContext.Current.Session[name] != null)
{
HttpContext.Current.Session[name] = value;
}
else
{
HttpContext.Current.Session.Add(name, value);
}
}
object Get(string name)
{
return HttpContext.Current.Session[name];
}
#endregion
}
我不明白這是可能的。界面只是合同的定義。將信息從類(我假設是代碼隱藏或服務器上的某些內容)傳遞到頁面(基於客戶端)涉及:a)cookie,b)視圖狀態,c)隱藏值或其他表單值, d)ajax電話,或e)其他一些逃避我的人。接口是一個定義,而不是一個機制。 – 2010-09-06 02:30:36
謝謝D.好,快問題。是否可以將Session中的STRUCT值添加爲值?例如 - SessionStates ss = new SessionStates(); ss.Username =「testing」; Session.Add(「MySession」,ss);但我不知道如何檢索它。 – jaysonragasa 2010-09-06 02:57:25
對不起:SessionStates ss =(SessionStates)Session [「MySession」]; – jaysonragasa 2010-09-06 03:03:04