Click here to Skip to main content
15,797,721 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Hi! 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();
    }
Posted
Updated 2-Sep-14 2:49am
v3

1 solution

As a lot before you - you missed the onbeforeunload event handler...
Read here: https://developer.mozilla.org/en-US/docs/WindowEventHandlers.onbeforeunload[^]
From that you can understand that the event handler is used to set the message displayed to user upon leaving the page...
At this point it not even sure he will leave the page at all as he can choose cancel too!
So - if your code WAS working you would be sorry for that...
Do a little article search in CP, you will find some help...
 
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