ramlicious Blogs by Tina & Prabhu

November 11, 2011

Notifying and Renewing Web sessions without AJAX

Filed under: .NET,C#,Programming,Uncategorized — Prabhuram @ 5:24 pm

Someone who tried to implement Notifying and Renewing Web sessions using AJAX using the Master pages would have learn’t that master pages doesn’t PageMethods and javascript will throw an error something like this

PageMethods is undefined

To overcome this, an easier way to retain the session is to simply use the server resource like calling an image in the server. So instead of using

PageMethods.RefreshSession(); //make the server call

we can simple do this

function triggerRefresh() {
var myImg = document.getElementById("imgDummy");
if (myImg) myImg.src = myImg.src.replace(/\?.*$/, '?' + Math.random());
}

which is to simply have an <img> tag. And assign a source to the image in my loadform() function like this:

            var myImg = document.getElementById("imgDummy")
            myImg.src = document.url + "?";

and so my complete script in the .aspx page will become like this:


            <!-- script added to implement session timeout logic
    Logic:  1.  If no keys were pressed by the user for (session time out period - 1) minutes, an alert will be
                shown one minute before the session time out, if the user responds to the message, the session
                refreshes, else the session expires eventually
            2.  If keys were pressed by the user before (session time out period - 1) minutes, then the session
                automatically refreshes.
        -->
    <script type="text/javascript" >
        var actionFlag; //flag to monitor user key actions
        var timeoutPeriod; //holder for the server session timeout period (in minutes)
        var int; //variable for Interval
        var seconds = 60 //constant
        var nanoseconds = 1000 //constant

        //Uses the PageMethods to get the session timeout from the server
        function GetSessionTimeout() {
            //PageMethods.GetTimeoutPeriod(function (result) { timeoutPeriod = result; }); //todo: make it synchronous instead of async
            timeoutPeriod = parseInt(document.forms[0].inttop.value);  //session time out is available in a hidden field

        }

        //Calculates the retrigger time
        function getRetriggerTime() {
            if (timeoutPeriod == 1) return timeoutPeriod;
            return timeoutPeriod - 1;
        }

        //function handler for form's load event
        function loadForm(oEvent) {
            actionFlag = false; //reset the flag
            GetSessionTimeout(); //get the session timeout
            int = setInterval("notifyTimeOut()", getRetriggerTime() * seconds * nanoseconds); //renew the interval
            var myImg = document.getElementById("imgDummy")
            myImg.src = document.url + "?";
        }

        //function that resets the actionFlag
        function keyDown(oEvent) {
            if (actionFlag) return; //skip if the flag is already true
            switch (oEvent.keyCode) {
                case 17: //skip if key is ctrl
                case 18: //skip if key is alt
                    return;
                    break;
            }
            //else
            actionFlag = true;
        }

        //function that refreshes the session without an alert
        function autoRefreshSession() {
            triggerRefresh();
            int = clearInterval(int); //clear the interval, todo: check if this line is required
            int = setInterval("notifyTimeOut()", getRetriggerTime() * seconds * nanoseconds);  //renew the interval
            alert('auto refresh');
        }

        //function that executes when the user triggers to refresh the session
        function manualRefreshSession() {
            triggerRefresh();
            int = setInterval("notifyTimeOut()", getRetriggerTime() * seconds * nanoseconds); //renew the interval
        }

        function triggerRefresh() {
            var myImg = document.getElementById("imgDummy");
            if (myImg) myImg.src = myImg.src.replace(/\?.*$/, '?' + Math.random());
        }
        //function thats executed at the scheduled time interval
        function notifyTimeOut() {
            int = clearInterval(int); //clear the interval, todo: check if this line is required
            if (actionFlag == true) autoRefreshSession(); //if action was taken, perform auto refresh
            else {
                alert('Your Session is about to expire! Click OK to refresh your session!'); //show alert to the user
                manualRefreshSession(); //if control returns refresh session, if the session has already expired a new session will start
            }
            actionFlag = false; //reset the flag
        }

    </script>

and I don’t have a single line of server code and most importantly no use for the ajax <scriptmanager>. Thanks to the author of Keep session alive with Javascript

No Comments

No comments yet.

RSS feed for comments on this post.

Sorry, the comment form is closed at this time.

Powered by WordPress