简体   繁体   English

当用户导航离开页面或退出浏览器时,如何在ASP.net中销毁会话

[英]How to destroy session in ASP.net when user navigates away from the page or exits the browser

I have three session that I am setting navigating from my homepage to another page, like this: 我有三个会话,我正在设置从我的主页导航到另一个页面,如下所示:

Session["LocationText"] = locText;
Session["SpecialtyText"] = speText;
Session["GenderText"] = genText;

Response.Redirect("anotherpage.aspx", false);

Once I am in anotherpage.aspx , and either I navigate away from the page or close the browser, I would like to destroy those variable. 一旦我在anotherpage.aspx ,并且我离开页面或关闭浏览器,我想销毁这些变量。 The whole idea is, if I come back to anotherpage.aspx and it wasn't redirected from the page from a button click, it will not have any sessions. 整个想法是,如果我回到anotherpage.aspx并且没有从按钮点击页面重定向,它将没有任何会话。

I tried the following: 我尝试了以下方法:

protected void Page_Unload(object sender, EventArgs e) {
    Session.Remove("LocationText");
    Session.Remove("SpecialtyText");
    Session.Remove("GenderText");
}

When I navigated away from the page and came back to it, it didn't destroy the variables. 当我离开页面并回到它时,它没有破坏变量。 How can I achieve what I am looking to do? 我怎样才能实现我的目标?

UPDATE : 更新

homepage: 主页:

Context.Items["LocationText"] = locText;
Context.Items["SpecialtyText"] = speText;
Context.Items["GenderText"] = genText;

anotherpage: 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();
}

Based on your question and comment, it sounds like Context may be more appropriate than Session . 根据您的问题和评论,听起来像Context可能比Session更合适。 Variables in Context only live for the life of the current request. Context变量仅适用于当前请求的生命周期。

Why not use Session.Abandon() on your Page_Unload event. 为什么不在Page_Unload事件上使用Session.Abandon() You can start a new session again. 您可以再次开始新会话。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 当用户在收到响应之前导航时,ASP.NET MVC控制器会发生什么? - What happens to an ASP.NET MVC controller when the user navigates away before a response is received? ASP.NET-我需要一个静态音乐播放器,无论用户是否导航到站点中的另一个页面,该静态音乐播放器都可以在页面上播放 - ASP.NET - I'm needing a static music player that remains on the page playing no matter if the user navigates away to another page within the site 浏览器如何识别ASP.NET C#中用户对浏览器的断开连接/导航 - How a browser can recognize a disconnection/navigate away from browser by user in ASP.NET C# 当用户从当前页面导航时,从会话中删除数据 - remove data from session when user navigates from current page ASP.Net C#破坏浏览器背面的表单会话 - ASP.Net C# Destroy Form Session on browser back 使用导航离开页面时如何清理 Blazor Server 应用程序中的内存 - How to clean up memory in Blazor Server app when using navigates away from a page 当用户单击asp.net中的浏览器后退按钮时,如何快速重定向页面 - How to redirect the page quickly when the user clicks the browser back button in asp.net 我如何从Session.net中的其他Web应用程序通过SessionId销毁Session? - How can I destroy Session by SessionId from different web application in asp.net? 如何在ASP.NET中使用LoggedOut时终止用户会话 - How to kill the Session of User when he LoggedOut in ASP.NET 当用户关闭浏览器(在asp.net中)时,如何通知Web服务器? - how to notify webserver when user closes the browser (in asp.net)?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM