Thursday, January 19, 2012

Keep Session Alive


<script language="javascript" type="text/javascript">
    var sessionTimeoutWarning = '<%= System.Configuration.ConfigurationSettings.AppSettings["SessionWarning"].ToString()%>'; //get Value from Webcofig
    var sessionTimeout = "<%= Session.Timeout %>"; //Get Sesstion TimeOut
    var timeOnPageLoad = new Date();
    var sessionWarningTimer = null;
    var redirectToWelcomePageTimer = null;
    //For warning
    var sessionWarningTimer;
    //To redirect to the welcome page
    var redirectToWelcomePageTimer;

    function CheckBrowserSession() {
        //For warning
        sessionWarningTimer = setTimeout('SessionWarning()', parseInt(sessionTimeoutWarning) * 60 * 1000);

        //To redirect to the welcome page
        redirectToWelcomePageTimer = setTimeout('RedirectToWelcomePage()', parseInt(sessionTimeout) * 60 * 1000);

    }
    //Session Warning
    function SessionWarning() {
        //minutes left for expiry
        var minutesForExpiry = (parseInt(sessionTimeout) - parseInt(sessionTimeoutWarning));
        var message = 'Your session will expire in another ' + minutesForExpiry + 'mins. Do you want to extend the session?';
        //Confirm the user if he wants to extend the session
        if (Confirm(message)) {
            if (redirectToWelcomePageTimer != null) {
                clearTimeout(redirectToWelcomePageTimer);
            }
            //reset the time on page load
            timeOnPageLoad = new Date();

            sessionWarningTimer = setTimeout('SessionWarning()', parseInt(sessionTimeoutWarning) * 60 * 1000);

            //To redirect to the welcome page
            redirectToWelcomePageTimer = setTimeout('RedirectToWelcomePage()', parseInt(sessionTimeout) * 60 * 1000);

        }
        //*************************
        //Even after clicking ok(extending session) or cancel button,
        //if the session time is over. Then exit the session.
        var currentTime = new Date();
        //time for expiry
        var timeForExpiry = timeOnPageLoad.setMinutes(timeOnPageLoad.getMinutes() + parseInt(sessionTimeout));

        //Current time is greater than the expiry time
        if (Date.parse(currentTime) > timeForExpiry) {
            alert("Session expired. You will be redirected to welcome page");
            window.location = "Login/index";
        }
        //**************************
    }

    //Session timeout
    function RedirectToWelcomePage() {
        alert("Session expired. You will be redirected to welcome page");
        window.location = "Login/index";
    }


  $(document).ready(function () {
            CheckBrowserSession()
   
            });

</script>

Tuesday, January 10, 2012

Validate File upload Control


function ValidateFileUpload(ControlID, filename) {

            var isValid = false;
            var Extension = filename.substring(filename.lastIndexOf('.') + 1).toLowerCase();
            var extArray = new Array("gif", "jpg", "tif", "jpeg", "tiff", "pdf", "txt", "doc", "docx", "rtf");

            for (var i = 0; i < extArray.length; i++) {
                if (extArray[i] == Extension) {
                    isValid = true;
                    break;
                }
            }
            if (isValid == false) {
                document.getElementById(ControlID).value = "";
                alert(' <%=ViewRes.Common.Invalidfileformat%>');
            }

        }


<input class="txtfld FileUpload" onchange="ValidateFileUpload(this.id,this.value)"
                            type="file" id="file1" name="AttributeImages1" />