简体   繁体   English

离开一页时如何清除会话

[英]How to clear Session when navigating away from one page

I googled this about 1/2 a hour no hit's. 我在Google上搜索了大约1/2小时,没有点击。 Scenario is that, dynamic scripts are saved in string builder whose "string" representation is stored in session. 场景是,动态脚本保存在字符串构建器中,其"string"表示形式存储在会话中。 It just happens that when user navigates away from one page to another the script[from session] gets registered using "RegisterStartupScript" . 恰好发生在用户从一个页面导航到另一页面时,[from session]脚本使用"RegisterStartupScript" The script is registered in PreRender event of the Page. 该脚本已在Page的PreRender事件中注册。 So i would like to clear this script in session while the page navigates away btw rule out a option to create another session variable and clear previous one. 因此,我想在会话中清除该脚本,同时页面导航结束,但排除了创建另一个会话变量并清除上一个变量的选项。 It's a overhead :( 这是一个开销:(

Why are you storing this in Session, do you need to maintain this script in between GET requests? 为什么将其存储在Session中,是否需要在GET请求之间维护此脚本?

If only postbacks are relevant you could store it in viewstate as this is maintained only when doing a postback. 如果仅回发是相关的,则可以将其存储在viewstate中,因为只有在进行回发时才维护此状态。

If you want this string to be available on GET requests too you might want to introduce a different variable which has an identifier identifying the page for which the script is generated. 如果您也希望该字符串在GET请求中可用,则可能需要引入一个不同的变量,该变量的标识符标识为其生成脚本的页面。 If the requested page doesn't match the control variable you will have to generate a new script. 如果请求的页面与控制变量不匹配,则必须生成一个新脚本。

您可以使用JavaScript onUnload()并调用AJAX服务,这将清除服务器端会话。

How is the user navigating away from the page? 用户如何离开页面导航? Can't you use an ASP.NET button instead of a hyperlink, and then do a Redirect in code once you have cleared your session variable? 您不能使用ASP.NET按钮而不是超链接,然后在清除会话变量后执行代码重定向吗?

protected void btnDoSomething_Click(object sender, EventArgs e)
{
    Session["Value"] = String.Empty;
    Response.Redirect(strURL, false);
}

OR You could add a variable in the query string and check it in the Page_Load event of the target page: 或者您可以在查询字符串中添加一个变量,然后在目标页面的Page_Load事件中检查它:

Webform1.aspx?reset=true

Since I cant comment yet, use onUnload(). 由于我还不能发表评论,请使用onUnload()。

It fires on full postbacks too. 它也会触发完整的回发。 Ajax postbacks dont fire! Ajax回发不触发!

What you need to do, is guaranty inside the onUload function that you only clear the session when you want. 您需要做的是确保onUload函数内部只有在需要时才清除会话。 Like setting a variable isPostBack to true before the postbacks so onUnload sees the variable and doenst send a request to clear the session. 就像在回发之前将变量isPostBack设置为true一样,因此onUnload会看到该变量并发出请求以清除会话。

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

相关问题 离开页面时记住级联下拉值 - Remember Cascading Dropdown value when navigating away from page 导航离开某个页面时执行c#命令 - Executing c# commands when navigating away from a certain page 如何在导航和返回页面时保留文本字段数据 - How to keep textfield data when navigating away and back to page 从内容页面移至另一个页面时如何清除会话? - how can clear session when i move from a content page to another one? 当用户导航离开页面或退出浏览器时,如何在ASP.net中销毁会话 - How to destroy session in ASP.net when user navigates away from the page or exits the browser Xamarin 滑块:“值是最大值的无效值”(仅当导航离开页面时) - Xamarin Slider: 'Value is an invalid value for Maximum' (Only when navigating away from a page) 从C#(.Net 3.5)页面导航时调用方法的方法? - Way to call a method when navigating away from a page in C# (.Net 3.5)? 离开页面之前,将当前URL保存在控制器中 - Saving current url in controller before navigating away from page 在Silverlight中,将导航远离包含线程的页面结束线程吗? - In Silverlight, will navigating away from the page that contains a thread end the thread? 从一个页面导航到另一页面时,Signalr注销用户 - Signalr logging out user when navigating from one page to another
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM