【发布时间】:2014-05-20 00:39:33
【问题描述】:
我设置了三个会话,从我的主页导航到另一个页面,如下所示:
Session["LocationText"] = locText;
Session["SpecialtyText"] = speText;
Session["GenderText"] = genText;
Response.Redirect("anotherpage.aspx", false);
一旦我进入anotherpage.aspx,我要么离开页面,要么关闭浏览器,我想销毁这些变量。整个想法是,如果我回到 anotherpage.aspx 并且它没有通过单击按钮从页面重定向,它将不会有任何会话。
我尝试了以下方法:
protected void Page_Unload(object sender, EventArgs e) {
Session.Remove("LocationText");
Session.Remove("SpecialtyText");
Session.Remove("GenderText");
}
当我离开页面并返回时,它并没有破坏变量。我怎样才能实现我想要做的事情?
更新:
主页:
Context.Items["LocationText"] = locText;
Context.Items["SpecialtyText"] = speText;
Context.Items["GenderText"] = genText;
另一页:
if (Context.Items["LocationText"] != null && Context.Items["SpecialtyText"] != null && Context.Items["GenderText"] != null) {
slcLocation.SelectedValue = Context.Items["LocationText"].ToString();
slcSpecialty.SelectedValue = Context.Items["SpecialtyText"].ToString();
slcGender.SelectedValue = Context.Items["GenderText"].ToString();
this.onBtnClick();
}
【问题讨论】:
-
"整个想法是,如果我回到 anotherpage.aspx,它不会有任何会话。"那么为什么首先将它们存储在会话中呢?
-
因为 anotherpage.aspx 使用这些会话变量来进行搜索查询。
-
尝试使用
Context而不是Session。这似乎更适合您的用例。 -
我是PHP背景,请举个例子:)
标签: c# asp.net session session-variables