Introduce DataBase,Asp.net,JavaScript,Xml,Html,Css,Sql,Php,ASP.NET Controls,AJAX,Tools,HTML,CSS,JavaScript,Open Source Project,WPF,.Net Framework,Linq
Top Recommended Hosting

Javascript show Session timeout counter

by the3factory 3/26/2008 6:25:00 PM
Download code
 

Introduction

To display the remaining minutes as a counter for a Session to timeout on the webpage that will change after every minute and a message will be displayed when the Session timeout will be equal to 0 (zero).

One thing to keep in consideration that the code provided will neither timeout the Session or it is responsible for any Session related activity. It will just show the timeout counter on the screen depending on the Session timeout set within application. If any additional things are required, additional code has to be written.

Background

It might be possible that in some of the application a developer wants to display the time left for the Session to over or timeout. That will enable the user to save the entered information or it can be used for any other purpose depending on the requirement.

Javascript that will be used to achieve this is:

 

<script type="text/javascript">
var sessionTimeout = "<%= Session.Timeout %>";
function DisplaySessionTimeout()
{
//assigning minutes left to Session timeout to Label
document.getElementById("<%= lblSessionTime.ClientID %>").innerText = sessionTimeout;
sessionTimeout = sessionTimeout - 1;
//if session is not less than 0
if (sessionTimeout >= 0)
//call the function again after 1 minute delay
window.setTimeout("DisplaySessionTimeout()", 60000);
else
{
//show message box
alert("Your current Session is over.");
}
}
</script>

 

Code on page load event to register the javascript method on startup:

 

protected void Page_Load(object sender, EventArgs e)
{
if (!IsPostBack)
{
//set DisplaySessionTimeout() as the startup script of this page
Page.ClientScript.RegisterStartupScript(this.GetType(),"onLoad","DisplaySessionTimeout()", true);
}
}

 

When application starts

After 1 minute

When session counter come to 0. Message box will be displayed.

Related posts

Sign up for PayPal and start accepting credit card payments instantly.


Powered by BlogEngine.NET 1.2.0.0