|
|
|
Title:
|
WebUser Control Javascript Session countdown timer
|
Language:
|
ASP.NET
|
|
Description:
|
Add this code to a user control and you have a countdown session timer for your asp.net page.
|
Views:
|
1208
|
|
Author:
|
Thomas Jenkins
|
Date Added:
|
11/7/2008
|
Copy
|
Code
|
|
using System;
using System.Data;
using System.Configuration;
using System.Collections;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using System.Text;
public partial class Controls_SessionTimer : System.Web.UI.UserControl
{
/// <summary>
/// Removes or Displays extra markup formatting for javascript.
/// Enable for production
/// </summary>
private bool _CompressJavascript = true;
public bool CompressJavascript
{
get
{
return _CompressJavascript;
}
set
{
_CompressJavascript = value;
}
}
protected void Page_Load(object sender, EventArgs e)
{
StringBuilder sb = new StringBuilder();
string tab = " ";
sb.AppendFormat(@"<script language=""Javascript"">{0}", Environment.NewLine);
sb.AppendFormat(@"var sessionTimeout = parseFloat('"+ Session.Timeout +"');{0}", Environment.NewLine);
sb.AppendFormat(@"{0}{0}function DisplaySessionTimeout(){1}", tab, Environment.NewLine);
sb.AppendFormat(@"{0}{0}{{{1}", tab, Environment.NewLine);
sb.AppendFormat(@"{0}{0}{0}document.getElementById('" + lblSessionTime.ClientID + "').innerHTML = parseFloat(sessionTimeout).toFixed(2);{1}", tab, Environment.NewLine);
sb.AppendFormat(@"{0}{0}{0}sessionTimeout = sessionTimeout - .01;{1}", tab, Environment.NewLine);
sb.AppendFormat(@"{0}{0}{0}{0}{1}", tab, Environment.NewLine);
sb.AppendFormat(@"{0}{0}{0}if (sessionTimeout >= 0){1}", tab, Environment.NewLine);
sb.AppendFormat(@"{0}{0}{0}{{{1}", tab, Environment.NewLine);
sb.AppendFormat(@"{0}{0}{0}{0}window.setTimeout(""DisplaySessionTimeout()"", 1000);{1}", tab, Environment.NewLine);
sb.AppendFormat(@"{0}{0}{0}}}{1}", tab, Environment.NewLine);
sb.AppendFormat(@"{0}{0}{0}else{1}", tab, Environment.NewLine);
sb.AppendFormat(@"{0}{0}{0}{{{1}", tab, Environment.NewLine);
sb.AppendFormat(@"{0}{0}{0}{0}alert(""Your Session has a minute remaining."");{1}", tab, Environment.NewLine);
sb.AppendFormat(@"{0}{0}{0}}}{1}", tab, Environment.NewLine);
sb.AppendFormat(@" }} ");
sb.AppendFormat(@"</script>");
if (_CompressJavascript)
{
Page.ClientScript.RegisterClientScriptBlock(this.Page.GetType(), this.ClientID + "SessionTimer", sb.Replace(" ", "").Replace(Environment.NewLine, "").ToString());
}
else
{
Page.ClientScript.RegisterClientScriptBlock(this.Page.GetType(), this.ClientID + "SessionTimer", sb.ToString());
}
}
}
|
|
Usage
|
<%@ Control Language="C#" AutoEventWireup="true" CodeFile="SessionTimer.ascx.cs" EnableViewState="false" Inherits="Controls_SessionTimer" %>
Session Timer: minutes remaining.
|
|
|