Tuesday, March 22, 2011

Session ticker with Javascript and C#

So, I built the awesomest application in the world and was stumped by a simple conundrum..

The users didnt realize they were getting timed out

So I found this gem... and modified it to be added to the master page


  var sessionTimeout = "<%= Session.Timeout %>";
  
        function DisplaySessionTimeout()
        {

            var sessionValue = '<%= Session["userID"] %>';
            //assigning minutes left to session timeout to Label
            if ((sessionValue != "")&&(sessionTimeout > 0)) {

                document.getElementById("<%= lblSessionTime.ClientID %>").innerText =
                                                                       "Session time left: " + sessionTimeout + " minutes";
                sessionTimeout = sessionTimeout - 1;

                //if session is not less than 0

                //call the function again after 1 minute delay
                window.setTimeout("DisplaySessionTimeout()", 60000);
            }
//            else {
//                //show message box
//                alert("Your current Session is over.");
//            }
        }




after adding this to the head.. I added this label to the body

    
        < asp: Label ID ="lblSessionTime" CssClass="showGreen" runat="server" >

and VOILA !! there is a green ticker updating every min informing people of the impending doom.. I mean session timeout

AH HAAA




No comments: