사이트가 있고 사용자가 사이트를 종료하려고 할 때 내 데이터베이스의 사용자 상태를 0 (오프라인)으로 설정하려고합니다. 문제는 Page_Unload 메소드를 시도했지만 시도하지 않았습니다. T 작업 및 일부 자바 스크립트 코드를 발견하지만이 자바 스크립트 코드는 사이트를 떠날 때 및이 코드에서 ASP.NET 메서드를 호출하여 데이터베이스의 데이터를 변경하지만 작업하지 않을 때 알림을 표시합니다. 감사합니다. 전진!자바 스크립트에서 ASP.NET 메서드 호출
// 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([email protected])", conexiune);
SetareOnline.Parameters.AddWithValue("@UserName", Session["Login"].ToString());
conexiune.Open();
SetareOnline.ExecuteNonQuery();
conexiune.Close();
}
페이지 방법은 정적이어야합니다. – Mairaj