views:

548

answers:

6

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>

Thank you, Nagu

+1  A: 

What you are attempting to do is a bad idea. Consider putting less data in the session, or lowering the timeout.

Perhaps you don't realise that onunload fires when the user refreshes or navigates away from the page. So assuming your code actually worked, if you user refreshed their page then their session would be terminated. If they visited any other page on the site their session would also be terminated!

Probably not the functionality you were hoping for!

RichardOD
A: 

No.. actually my intension is to handle the browser close event.

I want to clear the session when user closes the brower.

So I tried this

Nagu
Please don't create a new answer to respond to some's answer. Use the "add comment" link instead.
M4N
Can you add comments if your rep is below 50? I seem to recall that I couldn't until it got to 50.
Chris Dunaway
A: 

There is no way to consistently handle this type of event. There are too many causes for a session to "end". The very simplest of these would be a lost or closed connection. In this case any JavaScript fired off would never reach the server.

Even if you can get this working the way you want it is only going to work part of the time. If you can't rely on it your time may be spent and a different solution.

William Edmondson
A: 

It's been a few years, but I didnt know WebMethod can be static.

leppie
@Leppie- static is OK. This is really a Page Method not a Web service. It is just a way to expose functionality to MS AJAX.
RichardOD
@Leppie- you can read about them here- http://msdn.microsoft.com/en-us/magazine/cc163480.aspx#S5. Hope it is useful.
RichardOD
A: 

that means there is no solution for it or what?

Nagu
that means there is no way to detect the browser closed event. the body unload event occurs whenever the user clicks any link to leave your page, or refreshes the page, or anything.
Stobor
+2  A: 

I have tested with below code and it is working fine.

In Aspx page

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"&gt;
<html xmlns="http://www.w3.org/1999/xhtml"&gt;
<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>

In Codebehind

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();
    }

}

You can put breakpoint at AbandonSession method to verify that it is getting hit at unload.

Raghav Khunger