[英]What happens to an ASP.NET MVC controller when the user navigates away before a response is received?
[英]How to destroy session in ASP.net when user navigates away from the page or exits the browser
我有三個會話,我正在設置從我的主頁導航到另一個頁面,如下所示:
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;
anotherpage:
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();
}
根據您的問題和評論,聽起來像Context
可能比Session
更合適。 Context
變量僅適用於當前請求的生命周期。
為什么不在Page_Unload
事件上使用Session.Abandon()
。 您可以再次開始新會話。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.