Every programmer has the Question that how to handle sessions or kill
the sessions when browser close at client end? and how the server knows
when client close the browser?I have also searched on this a lot... and
find the solution... it may helpful to someone.
This can be achieved through the ajax.
we have a onunload event in the javascript,it will be fired when client close the connection.we need to add the
ScriptManager
control of ajax in .aspx page.. and set the EnablPageMethods to
true.(by setting the property we can call the web methods in server side
code).
sample.aspx page
<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
<title>Handle Sessions when close the browser</title>
</head>
<script type="text/javascript" language="javascript">
function
BrowserClose(){
PageMethods.BrowserCloseMethod();
}
</script>
<body onunload="
BrowserClose()">
<form id="form1" runat="server">
<div>
<asp:ScriptManager ID="ScriptManager1" runat="server"
EnablePageMethods="
true">
</asp:ScriptManager>
Testing
</div>
</form>
</body>
</html>
in the Code behind, we should put this code
[System.Web.Services.WebMethod]
public static void BrowserCloseMethod()
{
HttpContext.Current.Session.Abandon(); }