I have a site and i want to set the user status in my database to 0 (Offline) when the user want to exit the site , the problem is i tried Page_Unload method but doesn't work and i found some Javascript code which works but this Javascript code shows a notification when you want to leave the site and in this code i want to call an ASP.NET method to change data in database but doesn't work.Thanks in advance!
// Javascript code
window.onbeforeunload = confirmExit;
function confirmExit() {
PageMethods.OnlineOut();
}
//This is on my Master Page
<script type="text/javascript" src='<%= ResolveClientUrl("~/JavaScript.js") %>'></script>
<asp:ScriptManager ID="ScriptManager1" runat="server" EnablePageMethods="true">
</asp:ScriptManager>
//This is on my Content Page.cs
[WebMethod]
public void OnlineOut()
{
SqlConnection conexiune = new SqlConnection(ConfigurationManager.ConnectionStrings["ConnectionString"].ConnectionString);
SqlCommand SetareOnline = new SqlCommand("Update [dbo].[Table] Set Online=0 Where(UserName=@UserName)", conexiune);
SetareOnline.Parameters.AddWithValue("@UserName", Session["Login"].ToString());
conexiune.Open();
SetareOnline.ExecuteNonQuery();
conexiune.Close();
}