我想ASP.NET
在浏览器关闭时清除会话值。我在html
页面中定义了onunload函数,但是我不知道如何清除此Javascript方法中的会话值?
<body onunload="myUnloadFunction()">
<script>
function myUnloadFunction()
{
// Needs asp.net session code here
}
</script>
请让我知道是否还有其他其他可取的方式。
试试这个:
后台代码:
[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:
$.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 Page Methods
以及如何通过它们调用它们的更多信息,请AJAX
阅读使用jQuery直接调用ASP.NET AJAX页面方法
本文收集自互联网,转载请注明来源。
如有侵权,请联系[email protected] 删除。
我来说两句