Tracking Page Visiting information in asp.net
Recently I'm facing one issue with page log tracking. I tried in different ways but unsuccessful for Log out tracking, then after I found one solution by Searching in Google i.e. OnBeforeUnload event in javascript side.
In this article I'm trying to explain how to track the page logs i.e. login time and log out time by using asp.net, JavaScripts. This article will help you those who are looking for the same.
Tracking Page Visiting information in asp.net :
Recently I'm facing one issue with page log tracking. I tried in different ways but unsuccessful for Log out tracking, then after I found one solution by Searching in Google i.e. OnBeforeUnload event in javascript side.
In this article I'm trying to explain how to track the page logs i.e. login time and log out time by using asp.net, JavaScripts. This article will help you those who are looking for the same.
Follow below steps to achieve your goal.
Step 1:
Open Visual studio and create 2 pages in the name of Default.aspx and Default2.aspx.
Step2:
Open Default page and design the page like below.
<div>
<asp:HiddenField ID="hdnLogId" runat="server" />
<asp:Button ID="btnRedirect" runat="server" Text="Redirect" OnClick="btnRedirect_OnClick" />
</div>
Step3:
While click on Redirect button the page will redirect to other page name as "Default2.aspx". When the page is loaded I want to track the log in information against the user and insert it into database.
Default.aspx.cs
protected void Page_Load(object sender, EventArgs e)
{
if (!IsPostBack)
{
string pageName = this.Page.ToString().Split('.')[1];
string IPAddress = Request.UserHostAddress;
string UserId=// fetch login details;
DataSet ds = //insert log details into database.
if (ds.Tables.Count > 0)
{
hdnLogId.Value = ds.Tables[0].Rows[0][0].ToString();
}
}
}
Step4:
Now, I track the log in information. I want to update the log out details while leaving the page for that I called OnBeforeUnload event in client side scripting.
<script type="text/javascript" language="javascript">
$(document).ready(function () {
window.onbeforeunload = function () {
OnBeforeUnload();
}
}
function OnBeforeUnload() {
$.ajax({
type: "POST",
url: "Default.aspx/LogDetails",
data: "{ 'LogId':'" + $("#<%=hdnLogId.ClientID %>").val() + "' }",
contentType: "application/json; charset=utf-8",
dataType: "json",
success: function (data) {
response(data.d);
}
});
}
</script>
Step5:
Now, I'm clicking redirect button. When I click on redirect it's redirect to other page name as "Default2.aspx" while leaving time the page will call OnBeforeUnload function. While calling that function page will call the webmethod by passing the URL in ajax url with method name "LogDetails". In LogDetails I just perform the update the log out time based on log in user wise.
[System.Web.Services.WebMethodAttribute(), System.Web.Script.Services.ScriptMethodAttribute()]
public static DataSet LogDetails(string LogId)
{
//update the log out time based on log id of the particular user.
}Conclusion:
Hope this will helpful to those who are looking for tracking the log information based on logged users.