admin管理员组文章数量:1406725
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 session code here
}
</script>
Please let me know Is it possible else any other preferable way of doing.
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 session code here
}
</script>
Please let me know Is it possible else any other preferable way of doing.
Share Improve this question asked Jul 21, 2013 at 11:59 SmaugSmaug 2,6735 gold badges31 silver badges45 bronze badges 1- @KhanhTO - He cannot clear the session value in JavaScript, but he can call back to the server in his JavaScript code as shown in my answer. – Karl Anderson Commented Jul 21, 2013 at 12:08
1 Answer
Reset to default 5Try this:
Code-behind:
[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.
}
});
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
本文标签: cHow can I clear aspnet session value in OnUnload event in javascriptStack Overflow
版权声明:本文标题:c# - How can I clear asp.net session value in OnUnload event in javascript - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1745031343a2638514.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论