Click here to Skip to main content
15,899,026 members
Please Sign up or sign in to vote.
5.00/5 (1 vote)
See more:
Hi,

I am trying to show countdown by minute for the session. the session is set in the web.config for 10 minutes. I want to have on the master page the lbl to show 10, 9 , 8, ... in minutes.

here is the code for .cs

HTML
protected void Page_Load(object sender, EventArgs e)
{

    if (!IsPostBack)
    {
               Page.ClientScript.RegisterStartupScript(this.GetType(), "onLoad",         "DisplaySessionTimeout()", true);
    }

}


and in the .master file:

ASP.NET
<script type="text/javascript">
        var sessionTimeout = <%= Session.Timeout %>
        
        function DisplaySessionTimeout()
        {
            document.getElementById("<%= lblSessionTime.ClientID %>").innerText = sessionTimeout;
            sessionTimeout = sessionTimeout - 1;
            
            if (sessionTimeout >= 0)
                window.setTimeout("DisplaySessionTimeout()", 60000);
            else
            {
                alert("Your current Session is over.");
            }
        }
    </script>

       <div>
          <h2> Session time left: <asp:Label ID="lblSessionTime" runat="server"></asp:Label> minutes.</h2>
       <hr />
       </div>


Thanks.
Posted
Updated 3-Jan-16 20:48pm
v2

This approach will not work because every time your page loads the Session slides and the timeout is reset.

For example, if your session is set to 30 minutes and someone firsts loads the page then in 30 minutes the session will timeout if there is no activity. However, for your C# code to run the page must postback and if that happens after 10 minutes the session is reset and will timeout in 30 minutes, not in 20 minutes.

The way to do this is by using setTimeout() in JavaScript to run once a minute and at each minute the function will decrement 1. This is not a guarantee because you are not contacting the server to get the true timeout, but it is a crude attempt at doing something that technically isn't possible.

So, you could do something like this (just a simple example that you'll need to finish):
JavaScript
$(document).ready(function () {
// initial call of SessionTimeout
SessionTimeout(30);
});

function SessionTimeout(var minutesLeft)
{
$("#lblTimeout").val(minutesLeft); // display the number in a label
// call the function again in 1 minute and decrement the counter
setTimeout(function(){SessionTimeout(minutesLeft--);}, 1000);
}
 
Share this answer
 
This is a frequently asked question, but the client doesn't know when the session is going to expire so code like this is of very little value. I might have multiple tabs with the same session, there could be ajax calls keeping the session alive, I might have gone "back" to your page in the browser, the server might have aborted my session prematurely as it was running out of resources. The instances that stop this from working properly are so numerous, and the value of telling someone their session is over, means this is one of those things you just shouldn't bother doing.

Session timeouts should be handled only when they cause issues, such as Session variables no longer set etc.
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900