Click here to Skip to main content
15,914,403 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
My issue is


As user login i insert data in datbase to track the record of user's login

And in same way when user logout .Record is inserted in database to track
the logout record

thats why there is button to logout on the top of my site (that is Master page)

My issue is if user directly close the browser so how to insert the record in Database

My " Logout " button is on master page


when user close the browser so i want to update logout field in db(sql server)

thanks in advance
Posted

You may refer to the answers of this question that was asked sometime ago.

how to catch user logout status when browser is closed in c#.net[^]
 
Share this answer
 
yes it is possible with beforeunload() function.
refer:
Save Changes on Close of Browser or When Exiting the Page[^]
Save data while closing browser[^]
 
Share this answer
 
You can achieve the operation by trapping the browser close event programmatically using the below steps:

1. Create an instance of the ScriptManager as:
ASP.NET
<asp:scriptmanager id="ScriptManager1" runat="server" enablepagemethods="true" xmlns:asp="#unknown" />

2. Subscribe to the unload event of the body tag of the ASP.NET page
HTML
<body onunload="HandleClose()">
</body>

3. HandleClose function is placed within the Head section of the page as
JavaScript
<script language="javascript" type="text/javascript">
  function HandleClose() 
   {
     alert("Killing the session on the server!!");
      PageMethods.AbandonSession();
   }
</script>


4. On the server side, define the AbandonSession method and add to it the WebMethod attribute.
C#
[WebMethod]
public static void AbandonSession()
 {
   // user defined logout operation code
   HttpContext.Current.Session.Abandon();
 }
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900