离开一页时如何清除会话

发布于 2024-11-16 04:43:31 字数 226 浏览 0 评论 0原文

我用谷歌搜索了这个大约 1/2 小时没有命中。场景是,动态脚本保存在字符串生成器中,其 "string" 表示形式存储在会话中。碰巧的是,当用户从一个页面导航到另一页面时,脚本[来自会话]会使用“RegisterStartupScript”注册。该脚本在页面的 PreRender 事件中注册。因此,我想在页面导航时清除会话中的此脚本顺便说一句,排除创建另一个会话变量并清除前一个会话变量的选项。这是一个开销:(

I googled this about 1/2 a hour no hit's. Scenario is that, dynamic scripts are saved in string builder whose "string" representation is stored in session. It just happens that when user navigates away from one page to another the script[from session] gets registered using "RegisterStartupScript". The script is registered in PreRender event of the Page. 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 :(

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

那支青花 2024-11-23 04:43:31

为什么要将其存储在 Session 中,是否需要在 GET 请求之间维护此脚本?

如果只有回发相关,您可以将其存储在视图状态中,因为只有在进行回发时才会维护它。

如果您希望此字符串也可用于 GET 请求,您可能需要引入一个不同的变量,该变量具有标识为其生成脚本的页面的标识符。如果请求的页面与控制变量不匹配,您将必须生成一个新脚本。

Why are you storing this in Session, do you need to maintain this script in between GET requests?

If only postbacks are relevant you could store it in viewstate as this is maintained only when doing a postback.

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. If the requested page doesn't match the control variable you will have to generate a new script.

贪了杯 2024-11-23 04:43:31

用户如何离开页面?难道不能使用 ASP.NET 按钮而不是超链接,然后在清除会话变量后在代码中执行重定向吗?

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

或者您可以在查询字符串中添加一个变量并在目标页面的 Page_Load 事件中检查它:

Webform1.aspx?reset=true

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?

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:

Webform1.aspx?reset=true
╄→承喏 2024-11-23 04:43:31

由于我还不能发表评论,请使用 onUnload()。

它也会在完整回发时触发。 Ajax 回发不会触发!

您需要做的是在 onUload 函数内保证您只在需要时清除会话。就像在回发之前将变量 isPostBack 设置为 true 一样,onUnload 会看到该变量并不会发送清除会话的请求。

Since I cant comment yet, use onUnload().

It fires on full postbacks too. Ajax postbacks dont fire!

What you need to do, is guaranty inside the onUload function that you only clear the session when you want. Like setting a variable isPostBack to true before the postbacks so onUnload sees the variable and doenst send a request to clear the session.

笑梦风尘 2024-11-23 04:43:31

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

You may use the JavaScript onUnload() and call an AJAX service, that will clear the server side session.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文