Below is my aspx code with a script function to fire the button with the method.
<%@ Page Language="C#" AutoEventWireup="true" CodeBehind="Global.aspx.cs" Inherits="TestDropdowns.Global1" %>
<!DOCTYPE html>
<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
<script type="text/javascript">
//<![CDATA[
function HandleClose() {
alert("Killing the session on the server from Global.aspx!!");
document.getElementById('Button1').click();
//PageMethods.AbandonSession();
}
</script>
<title></title>
</head>
<body onbeforeunload="HandleClose()"> <%--onbeforeunload="HandleClose()"--%>
<form id="form1" runat="server">
<div>
<asp:scriptmanager id="ScriptManager1" runat="server" enablepagemethods="true" />
</div>
<asp:Button ID="Button1" runat="server" Text="CreateLog" Height="24px" OnClick="Button1_Click" />
</form>
</body>
</html>
This works fine. but if I can the script to the following
<script type="text/javascript">
//<![CDATA[
function HandleClose() {
alert("Killing the session on the server from Global.aspx!!");
//document.getElementById('Button1').click();
PageMethods.AbandonSession();
}
</script>
The AbandonSession() does not fire,
Could some point me in the right direction of what I'm doing wrong please. I just want to fire off the method on server side when the browser is closed unexpectedly or via the X button.
The issue is likely that your async PageMethods is not even firred before the document is unloaded.
I believe it should be window.onbeforeunload (else it's the document rather than the window)
IMO sometimes this kind of little enhancement is counter productive. If a user opened a new tab, you'll close the session as well while a window is still opened.
Are you sure that your first method is feasible, because as far as I know, the alert method has been ignored in the onbeforeunload event in HTML5. You can see this at here.
Generally, we use return ("xxx") to display the prompt information.
You can try using the following code to invoke the background method:
Notice: Please make sure your AbandonSession method is workable.
<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
<script src="Scripts/jquery-3.3.1.min.js"></script>
<script type="text/javascript">
//<![CDATA[
function HandleClose() {
//alert('Killing the session on the server from Global.aspx!!');
PageMethods.AbandonSession(OnSuccess);//put pageMethods before return
//document.getElementById('Button1').click();
return ("Killing the session on the server from Global.aspx!!");
}
function OnSuccess() {
}
</script>
<title></title>
</head>
<body onbeforeunload="return HandleClose()">
<%--onbeforeunload="HandleClose()"--%>
<form id="form1" runat="server">
<div>
<asp:ScriptManager ID="ScriptManager1" runat="server" EnablePageMethods="true" />
<asp:Button ID="Button1" runat="server" Text="CreateLog" Height="24px" OnClick="Button1_Click" />
</div>
</form>
</body>
</html>
Thank you for your reply and suggestion, sorry its taken me so long to get back ive been on holiday.
I have changed my code to the following but the method is not firing, I have a button to fire off my method and that works ok so I know the method is ok.
The HandleClose function is not being called as I don't even get the "Killing the session on the server from Global.aspx!!" message.
Server memory is cleaned up when the user's Session times out on the server. The Global.asax has a Session_End event where you can execute code in response to a Session timeout.
Basically, you need to rethink the design based on how ASP.NET and Browsers work.
This error is because you used jQuerry but didn't refer to jquery.js.
Please add your local jquery reference or an online one.
For exacmple:
<head runat="server">
<title></title>
<script src="Scripts/jquery-3.3.1.min.js"></script><%--This is my local jquery.js--%>
<%--<script src="https://code.jquery.com/jquery-3.1.1.min.js"></script>--%><%--This is jquery online reference--%>
</head>
Hi, I have taken a different approach but I am still not getting anywhere
Modern browser do not send AJAX request when the browser is closing. Doing so can cause the browser to stay open waiting for a response which is not user friendly.
As stated above, Session is lost when the user closes the browser. Move your code to the Session_End in the Global.asax. The Session_End event, for InProc Session, fires when the Session times out on the server.
Member
35 Points
375 Posts
Issue firing Server side method when browser closes via X button
Jul 07, 2019 07:47 PM|masterdineen|LINK
Hello there I have the following method with a [Webmethod] attribute and a button to fire the method
Below is my aspx code with a script function to fire the button with the method.
This works fine. but if I can the script to the following
The AbandonSession() does not fire,
Could some point me in the right direction of what I'm doing wrong please. I just want to fire off the method on server side when the browser is closed unexpectedly or via the X button.
Regards Rob
All-Star
48520 Points
18073 Posts
Re: Issue firing Server side method when browser closes via X button
Jul 08, 2019 07:40 AM|PatriceSc|LINK
Hi,
Try https://developer.mozilla.org/en-US/docs/Web/API/Navigator/sendBeacon or a sync Ajax call.
The issue is likely that your async PageMethods is not even firred before the document is unloaded.
I believe it should be window.onbeforeunload (else it's the document rather than the window)
IMO sometimes this kind of little enhancement is counter productive. If a user opened a new tab, you'll close the session as well while a window is still opened.
Contributor
3140 Points
983 Posts
Re: Issue firing Server side method when browser closes via X button
Jul 08, 2019 07:59 AM|Yang Shen|LINK
Hi masterdineen,
Are you sure that your first method is feasible, because as far as I know, the alert method has been ignored in the onbeforeunload event in HTML5. You can see this at here.
Generally, we use return ("xxx") to display the prompt information.
You can try using the following code to invoke the background method:
Notice: Please make sure your AbandonSession method is workable.
Best Regard,
Yang Shen
Member
35 Points
375 Posts
Re: Issue firing Server side method when browser closes via X button
Jul 28, 2019 09:09 PM|masterdineen|LINK
Hello Yang
Thank you for your reply and suggestion, sorry its taken me so long to get back ive been on holiday.
I have changed my code to the following but the method is not firing, I have a button to fire off my method and that works ok so I know the method is ok.
The HandleClose function is not being called as I don't even get the "Killing the session on the server from Global.aspx!!" message.
<%@ Page Language="C#" AutoEventWireup="true" CodeBehind="Global.aspx.cs" Inherits="TestDropdowns.Global1" %>
<!DOCTYPE html>
<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
<script type="text/javascript">
//<![CDATA[
function HandleClose() {
//document.getElementById('Button1').click();
PageMethods.AbandonSession();
//return ("Killing the session on the server from Global.aspx!!");
}
window.onbeforeunload = HandleClose();{
return ("Killing the session on the server from Global.aspx!!");
}
</script>
<title></title>
</head>
<body>
<form id="form1" runat="server">
<div>
<asp:scriptmanager id="ScriptManager1" runat="server" enablepagemethods="true" />
</div>
<asp:Button ID="Button1" runat="server" Text="CreateLog" Height="24px" OnClick="Button1_Click" />
</form>
</body>
</html>
All-Star
53041 Points
23618 Posts
Re: Issue firing Server side method when browser closes via X button
Jul 29, 2019 06:07 PM|mgebhard|LINK
Session is lost once the browser is closed.
Server memory is cleaned up when the user's Session times out on the server. The Global.asax has a Session_End event where you can execute code in response to a Session timeout.
Basically, you need to rethink the design based on how ASP.NET and Browsers work.
Member
35 Points
375 Posts
Re: Issue firing Server side method when browser closes via X button
Jul 30, 2019 09:20 PM|masterdineen|LINK
Hi, I have taken a different approach but I am still not getting anywhere
see code below.
aspx code below
AbandonSession() function C# code below
When I run my app I get the following error
Line: 15
Error: '$' is undefined
could someone help please
Contributor
3140 Points
983 Posts
Re: Issue firing Server side method when browser closes via X button
Jul 31, 2019 07:45 AM|Yang Shen|LINK
Hi masterdineen,
This error is because you used jQuerry but didn't refer to jquery.js.
Please add your local jquery reference or an online one.
For exacmple:
Best Regard,
Yang Shen
All-Star
53041 Points
23618 Posts
Re: Issue firing Server side method when browser closes via X button
Jul 31, 2019 10:57 AM|mgebhard|LINK
Modern browser do not send AJAX request when the browser is closing. Doing so can cause the browser to stay open waiting for a response which is not user friendly.
As stated above, Session is lost when the user closes the browser. Move your code to the Session_End in the Global.asax. The Session_End event, for InProc Session, fires when the Session times out on the server.