我想了解緩存,頁面視圖狀態和會話。我在VS2010中創建了一個ASP.NET Web應用程序,並在頁面中添加了3個文本框和一個按鈕。我運行在調試模式下,輸入隨機文本到每個文本中,按下按鈕,似乎沒有任何東西被保存(所有文本都是「null」,就像你在代碼中看到的那樣)。我是否在錯誤的地方執行了這些操作?我需要添加一些東西到web.config?下面是我使用的代碼:使用ASP.NET緩存/ ViewState /會話
protected void Page_Load(object sender, EventArgs e)
{
if (IsPostBack)
{
if (Cache["textbox1"] != null)
TextBox1.Text = (string)Cache["textbox1"];
else
TextBox1.Text = "null";
if (ViewState["textbox2"] != null)
TextBox2.Text = (string)ViewState["textbox2"];
else
TextBox2.Text = "null";
if (Session["textbox3"] != null)
TextBox3.Text = (string)Session["textbox3"];
else
TextBox3.Text = "null";
}
}
protected void Button1_Click(object sender, EventArgs e)
{
Cache["textbox1"] = "(Cache) " + TextBox1.Text;
ViewState["textbox2"] = "(VS) " + TextBox2.Text;
Session["textbox3"] = "(Session) " + TextBox3.Text;
}
和頁面標題:
<%@ Page Language="C#" AutoEventWireup="true" CodeBehind="Default.aspx.cs" Inherits="State._Default" EnableSessionState="True" EnableViewState="True" %>
謝謝,對不起菜鳥問題。我對此很陌生。
你介意發佈Default.aspx代碼的其餘部分嗎? – 2011-04-26 16:19:38