如何在javascript中清除OnUnload事件中的asp.net会话值
How can I clear asp.net session value in OnUnload event in javascript
我想在浏览器关闭时清除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
调用它们的更多信息,请阅读Using jQuery to directly call ASP.NET AJAX page methods
相关文章:
- 借助asp.net验证或java脚本对多个文本进行验证
- 将Javascript数组发送到控制器ASP.NET MVC
- ASP.NET通过单击JavaScript按钮触发c#事件
- 文本框不是从Javascript/Asp.net中的对话框中打印出来的
- 从javascript调用asp.net codebehind函数
- 使用javascript的Asp.net内容占位符
- 获取ASP.NET Ajax Timer状态
- ASP.NET网络摄像头显示
- 如何在只能有一个asp.net表单的主页上从asp.net页面中的javascript中获取值
- 如何将乳胶配方奶粉图像保存到Asp.net中的文件夹中
- asp.net,包括iss上的javascript slow
- 选择不在GridView-ASP.NET中激发
- 什么'是在asp.net MVC中将本地化的resources.resx文件转换为javascript文件的有效
- 使用ASP.NET将谷歌地图添加到项目中,并从数据库中检索位置
- 有什么“;错误的”;将javascript放在asp.net表单上,而不是放在单独的文件中
- 将Javascript日期转换为ASP.NET日期格式
- C#asp.net mvc Set CheckBoxFor已检查的具有Model Value的属性
- 在asp.net中确认是或否消息
- ASP.NET MVC 3-在ajax调用后,重定向到新页面或生成页面刷新
- asp.net中的验证工具