[英]Declaring a global variable in ASP.NET codebehind
我想聲明一個Dictionary<string, object>
變量,但是不知道在哪里/如何去做。 字典中的值將是Page中的對象(ListBoxes,DropDownLists等),因此我無法在其他地方完全創建幫助器類。 有什么辦法可以讓我從后面的代碼中的每個方法訪問此變量?
您可以在三個地方存儲數據。 在應用程序級別,這使得所有會話都可以訪問數據。 會話級別,使數據可用於該特定用戶的所有頁面。 或者,頁面級別,使其在回發之間可用於當前頁面。 請參閱以下示例:
在應用程序級別存儲
封裝存儲的樣本類:
public static class ApplicationData
{
private static Dictionary<string, object> _someData = new Dictionary<string, object>();
public static Dictionary<string, object> SomeData
{
get
{
return _someData;
}
}
}
使用示例(在頁面加載事件中):這將在所有會話中增加該值。 要嘗試,請在您的計算機上打開兩個瀏覽器,並且使用相同的URL。 請注意,如何針對每個用戶的請求增加值。
// Application Data Usage
if (ApplicationData.SomeData.ContainsKey("AppKey"))
{
ApplicationData.SomeData["AppKey"] = (int)ApplicationData.SomeData["AppKey"] + 1;
}
else
{
ApplicationData.SomeData["AppKey"] = 1;
}
Response.Write("App Data " + (int)ApplicationData.SomeData["AppKey"] + "<br />");
在會話級別存儲:用於封裝存儲的示例類:
public class SessionData
{
private Dictionary<string, object> _someData;
private SessionData()
{
_someData = new Dictionary<string, object>();
}
public static Dictionary<string, object> SomeData
{
get
{
SessionData sessionData = (SessionData)HttpContext.Current.Session["sessionData"];
if (sessionData == null)
{
sessionData = new SessionData();
HttpContext.Current.Session["sessionData"] = sessionData;
}
return sessionData._someData;
}
}
}
用法示例(在頁面加載事件中):當前用戶會話的值增加。 當服務器上正在運行另一個會話時,它將不會增加。
// Session Data Usage.
if (SessionData.SomeData.ContainsKey("SessionKey"))
{
SessionData.SomeData["SessionKey"] = (int)SessionData.SomeData["SessionKey"] + 1;
}
else
{
SessionData.SomeData["SessionKey"] = 1;
}
Response.Write("Session Data " + (int)SessionData.SomeData["SessionKey"] + "<br />");
頁面級存儲
在頁面內:
private Dictionary<string, int> _someData;
private Dictionary<string, int> SomeData
{
get
{
if (_someData == null)
{
_someData = (Dictionary<string, int>)ViewState["someData"];
if (_someData == null)
{
_someData = new Dictionary<string, int>();
ViewState.Add("someData", _someData);
}
}
return _someData;
}
}
樣品用量
在頁面加載處理程序中
if (!this.IsPostBack)
{
incrementPageState();
Response.Write("Page Data " + SomeData["myKey"] + "<br />");
}
private void incrementPageState()
{
// Page Data Usage
if (SomeData.ContainsKey("myKey"))
{
SomeData["myKey"] = SomeData["myKey"] + 1;
}
else
{
SomeData["myKey"] = 1;
}
}
在按鈕上單擊:
protected void hello_Click(object sender, EventArgs e)
{
incrementPageState();
Response.Write("Page Data " + SomeData["myKey"] + "<br />");
}
請記住,ViewState不會在頁面加載時反序列化,但是會在事件處理程序(如Button.Click)中反序列化。
所有代碼均已通過測試,如果您需要完整的解決方案,請告訴我,我會將其通過電子郵件發送給您。
在類內部但在任何方法外部聲明變量。 例如:
namespace WebApplication1
{
public partial class _Default : System.Web.UI.Page
{
private Dictionary<string, object> myDictionary;
protected void Page_Load(object sender, EventArgs e)
{
myDictionary = new Dictionary<string, object>();
myDictionary.Add("test", "Some Test String as Object");
}
protected void TextBox1_TextChanged(object sender, EventArgs e)
{
myDictionary.Add("TextBox1Value", TextBox1.Text);
}
protected void Button1_Click(object sender, EventArgs e)
{
TextBox1.Text = myDictionary["test"].ToString();
}
}
}
關於什么類型的數據,要存儲多長時間等有多種選擇。查看會話狀態 , ViewState , 應用程序狀態
根據您的全局變量需求,我可以想到兩種可能性。
使用靜態類和靜態變量,如下面的代碼所示。
internal static class GlobalData { public static Dictionary<string, object> SomeData { get; set; } }
現在使用它
//initialize it once in Application_Start or something.
GlobalData.SomeData = new Dictionary<string, object>();
//use it wherever you want.
object o = GlobalData.SomeData["abc"];
2使用Application
狀態存儲您的全局數據。 如下。
//Store it in application state
Application["YourObjectUniqueName"] = new Dictionary<string, object>();
//access it wherever using
Application["YourObjectUniqueName"]["abc"]
您可以創建一個必須是公共的類文件(例如general.cs),以便可以輕松訪問它。 在該類文件中,您可以定義此全局變量。
您可以從其他任何頁面或類中訪問此變量,因為它是公共定義的,並且可以通過創建類的實例來利用此全局變量。
您可以在類聲明行之后的行后面的代碼中聲明變量。 如果您只想在一頁上使用它,它將起作用。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.