Cookie
//将数据保存到Cookie
//Cookie是一个字典
//如果没有指定Cookie的保存时间,则这个Cookie是临时Cookie
//临时Cookie只要浏览器一关闭,就取消
//Response.Cookies["account"].Value = txt1.Text;
//如果指定了Cookie的保存时间,这个Cookie就叫持久Cookie
Response.Cookies["account"].Value =
txt1.Text;
Response.Cookies["account"].Expires = DateTime.Now.AddMinutes(1);
//Response.Cookies["account"].Expires = DateTime.Now.AddMinutes(-1);
if
(Request.Cookies["account"] != null)
{
lab1.Text = Request.Cookies["account"].Value;
}
ViewState
利用ViewState保持页面状态_页面状态对象
protectedvoid btn3_Click(object sender, EventArgs e)
{
a = 10;
b = 20;
//ViewState是一个字典,键值对
//ViewState用于本页面回发时保持状态
ViewState["a"] = a;
ViewState["b"] = b;
}
protectedvoid btn4_Click(object sender, EventArgs e)
{
a = Convert.ToInt32(ViewState["a"]);
b = Convert.ToInt32(ViewState["b"]);
c = a + b;
lab2.Text = c.ToString();
}
Session
Session["username"] = txt1.Text;
if (Session["username"] != null)
{
lab1.Text = Session["username"].ToString();
}
Application公共对象
//因为Application对象时公共对象
//当多个用户同时修改Application对象时,可能会引发不可预知的问题
//并发
Application.Lock();//锁定对象
//Application.Add("account", txt1.Text);
Application["account"] = txt1.Text;
//...
Application.UnLock();//解锁对象
if (Application["account"] != null)
{
lab1.Text = Application["account"].ToString();
}
//ViewState是一个字典,键值对
//ViewState用于本页面回发时保持状态
ViewState["a"] = a;
ViewState["b"] = b;