浏览器关闭时调用页面方法 [英] Calling a Page Method when the Browser Closes
本文介绍了浏览器关闭时调用页面方法的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
您好,我正在尝试在bodyunload方法上调用[webmethod].
Hi here I'm trying to call a [webmethod] on bodyunload method.
但是它仅在页面加载本身上被触发.我该如何预防?
But it is getting fired on page load itself only. How do i prevent it?
这是我正在使用的代码:
Here's the code I am using:
[WebMethod]
public static void AbandonSession()
{
HttpContext.Current.Session.Abandon();
}
<script language="javascript" type="text/javascript">
//<![CDATA[
function HandleClose() {
PageMethods.AbandonSession();
}
//]]>
</script>
<body onunload="HandleClose()">
....
....
....
</body>
谢谢你, 那古(Nagu)
Thank you, Nagu
推荐答案
我已经使用以下代码进行了测试,并且工作正常.
I have tested with below code and it is working fine.
在Aspx页面中
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
<title></title>
<script type="text/javascript">
//<![CDATA[
function HandleClose()
{
PageMethods.AbandonSession();
}
//]]>
</script>
</head>
<body onunload="HandleClose()">
<form id="form1" runat="server">
<asp:ScriptManager ID="ScriptManager1" runat="server" EnablePageMethods="true">
</asp:ScriptManager>
</form>
</body>
</html>
隐藏在代码后
using System;
using System.Collections.Generic;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.Services;
public partial class ClearSessionOnPageUnload : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
}
[WebMethod]
public static void AbandonSession()
{
HttpContext.Current.Session.Abandon();
}
}
您可以在AbandonSession方法上放置断点,以验证它是否在卸载时被命中.
You can put breakpoint at AbandonSession method to verify that it is getting hit at unload.
这篇关于浏览器关闭时调用页面方法的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文