如何在JavaScript中的OnUnload事件中清除asp.net会话值 [英] How can I clear asp.net session value in OnUnload event in javascript

查看:55
本文介绍了如何在JavaScript中的OnUnload事件中清除asp.net会话值的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想在浏览器关闭时清除 ASP.NET 会话值.我在 html 页面中定义了onunload函数,但是我不知道如何在此Javascript方法中清除会话值?

I'd like to clear ASP.NET session value when browser closed. I've onunload function defined in my html page, but I don't know how can I clear session value in this Javascript method ?

<body onunload="myUnloadFunction()">


<script>
function myUnloadFunction()
{
  // Needs asp.net session code here
}
</script>

请让我知道,还有其他可行的方法吗?

Please let me know Is it possible else any other preferable way of doing.

推荐答案

尝试一下:

隐藏代码:

[WebMethod]
public static void ClearYourSessionValue()
{
    // This will leave the key in the Session cache, but clear the value
    Session["YourKey"] = null;

    // This will remove both the key and value from the Session cache
    Session.Remove("YourKey");
}

JavaScript:

JavaScript:

$.ajax({
    type: "POST",
    url: "PageName.aspx/ClearYourSessionValue",
    data: "{}",
    contentType: "application/json; charset=utf-8",
    dataType: "json",
    success: function(msg) {
         // Do something interesting here.
    }
});

如果您想了解有关 ASP.NET AJAX页面方法以及如何通过 AJAX 调用它们的更多信息,请阅读

If you want more information about ASP.NET AJAX Page Methods and how to invoke them via AJAX then read Using jQuery to directly call ASP.NET AJAX page methods

这篇关于如何在JavaScript中的OnUnload事件中清除asp.net会话值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆