hi all My application is also have master and child pages. I am trying to get the logout time when browser is closed, I have tried to get the time in master page but it will not get the time in browser close when i put the code under master page
onbeforeunload event. so i have changed my code to put the getting time code in all chaild pages
onbeforeunload event it works fine.
So,i have tried with this solution, its works fine with me..
first create any .aspx page and in page load write which code do you want to run at the time of browser close.
I have done for getting the logout time and update it into log table. Below is my sample code for logout.aspx page
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Data.SqlClient;
using System.Windows;
using System.Configuration;
using System.Web.Services;
public partial class logout : System.Web.UI.Page
{
public string strConstr = ConfigurationManager.ConnectionStrings["myConnectionString"].ConnectionString;
public DateTime utcTime = DateTime.UtcNow;
public TimeZoneInfo tzi = TimeZoneInfo.FindSystemTimeZoneById("India Standard Time");
public DateTime localTime;
public int loginId;
protected void Page_Load(object sender, EventArgs e)
{
localTime = TimeZoneInfo.ConvertTimeFromUtc(utcTime, tzi);
loginId = Convert.ToInt32(Session["LoginId"].ToString());
try
{
DateTime dtnow = localTime;
SqlConnection cn = new SqlConnection(strConstr);
cn.Open();
SqlCommand cmd = new SqlCommand("updateLogoutTime", cn);
cmd.CommandType = System.Data.CommandType.StoredProcedure;
cmd.Parameters.Add(new SqlParameter("LoginId", Convert.ToInt32(Session["LoginId"])));
cmd.Parameters.Add(new SqlParameter("outDate", dtnow.Date));
cmd.Parameters.Add(new SqlParameter("outTime", dtnow.ToString("HH:mm:ss tt")));
cmd.ExecuteNonQuery();
}
catch (Exception ex)
{
Show(ex.Message.ToString());
}
}
public void Show(string msg)
{
Page page = HttpContext.Current.Handler as Page;
if (page != null)
{
ScriptManager.RegisterStartupScript(page, page.GetType(), "msg", "alert('" + msg + "');", true);
}
}
}
and need to call the logout.aspx page in all other aspx pages of onbeforeunload event
like:
$(document).ready(function () {
window.onbeforeunload = function () { updateLogout(); };
});
function updateLogout() {
$.ajax({
url: "logout.aspx",
contentType: "application/json; charset=utf-8",
type: "POST",
success: function (data) {
},
error: function (x, y, z) {
}
});
}
and need to put
scriptmanager in all child pages to work the above script.