Session Timeout Not Redirecting

8 posts, 0 answers
  1. Tawnya
    Tawnya avatar
    6 posts
    Member since:
    Nov 2011

    Posted 30 Dec 2011 Link to this post

    Following the directions from this demo (http://demos.telerik.com/aspnet-ajax/notification/examples/sessiontimeout/defaultcs.aspx), I've managed to get my radnotification to popup and when the button is clicked, extend the session. I did have to change the radbutton to a standard button because it would not fire when clicked.

    However, I am unable to get the time label to update on the notification nor can it redirect to the page set in the value property. I will post the code to show what I have so far.

    <telerik:RadNotification
                               ID="rnSessionTimeOut" runat="server" Skin="Black"  AutoCloseDelay="60000"
           Title="Continue Your Session" TitleIcon=""  EnableRoundedCorners="true" ShowInterval="61000"
                               Value="Search.aspx" OnCallbackUpdate="OnCallbackUpdate" Position="Center">
             <ContentTemplate>
               <div class="infoIcon">
                   <img id="Img1" src="~/images/infoIcon.png" runat="server" alt="info icon" /></div>
               <div class="notificationContent">
                   Time remaining:  <span id="timeLbl">60</span>
                   <asp:Button ID="btnContinue" runat="server" Text="Continue Session" OnClientClick="ContinueSession(); return false;" />
                               
               </div>
           </ContentTemplate>
                           </telerik:RadNotification>

    in the javascript, I have this:

    <script type="text/javascript">
        
      
     
        //update the text in the label in RadNotification
        //this could also be done automatically by using UpdateInterval. However, this will cause callbacks [which is the second best solution than javascript] on every second that is being count
        function UpdateTimeLabel(toReset) {
            
            var sessionExpired = (seconds == 0);
            if (seconds == 0) {
                stopTimer("timeLeftCounter");
                //redirect to session expired page - simply take the url which RadNotification sent from the server to the client as value
                window.location.href = $find("RadNotification1").get_value();
            }
            else {
                var timeLbl = $get("timeLbl");
                timeLbl.innerHTML = seconds--;
            }
        }
     
        function ContinueSession() {
             
            var notification = $find("rnSessionTimeOut");
            //we need to contact the server to restart the Session - the fastest way is via callback
            //calling update() automatically performs the callback, no need for any additional code or control
            notification.update();
            notification.hide();
     
            //resets the showInterval for the scenario where the Notification is not disposed (e.g. an AJAX request is made)
            //You need to inject a call to the ContinueSession() function from the code behind in such a request
            var showIntervalStorage = notification.get_showInterval(); //store the original value
            notification.set_showInterval(0); //change the timer to avoid untimely showing, 0 disables automatic showing
            notification.set_showInterval(showIntervalStorage); //sets back the original interval which will start counting from its full value again
     
            stopTimer("timeLeftCounter");
            seconds = 60;
            updateMainLabel(true);
        }
     
    </script>

    And I have a sub for the OnCallBackUpdate that is empty:
       
    Sub OnCallbackUpdate()

    End Sub


    Basically, I have everything like the example. I set the showInterval and AutoCloseDelay set low for testing purposes.

    Any ideas? I've researched for days and haven't figured out why it isn't working. 

    I did add an alert temporarily to the code that updates the label and it never hits it except when I set it to the onclientclicked event. But, it only hits it, it doesn't execute it.





  2. Svetlina Anati
    Admin
    Svetlina Anati avatar
    2795 posts
    Member since:
    Sep 2012

    Posted 03 Jan 2012 Link to this post

    Hi Tawnya,

    I examined your code and here are some notes I prepared for you:

    1) You have changed the ID of the notification but you still have RadNotification1 used to find a referenced - please make sure that you have used the correct ID.

    2) You have set AutoCloseDelay to a random preferred value - please note that it should be calculated depending on the session timeout as we have done in the server code of the demo.

    3) The functionality itself works without the javascript for timers and labels update. However, the labels are part of the UI, not the functionality itself and if you want to use the same UI, you should use that code as well.

    Here are some additional explanations and details of the used logic:

    The code which is commented is for the labels - the one on the main page and the one inside the notification. If you remove it the other code should continue working but you should also remove any calls to those functions or UI elements. The result will be that you will have to wait without having the visual indicator.


    As to your particular questions - I prepared some notes which could be helpful:

    1) The Session is set to 2 min in the code behind:

    Session.Timeout = 2;

    2) The RadNotification is configured to automatically show 1 minute before the session times out (note that the session timeout can be set only in minutes while the notification is configured in milliseconds):

    RadNotification1.ShowInterval = (Session.Timeout - 1) * 60000;

    3) The Value property of the notification is set to the desired url for redirect:

    RadNotification1.Value = Page.ResolveClientUrl("SessionExpired.aspx");

    What happens next is that the notification shows due to 2) and then there are 2 scenarios:

    4.1) The user does nothing - in this case the session expires (more information for Session is available here: http://msdn.microsoft.com/en-us/library/ms178581.aspx) and in this case we redirect to the other page by using this code:

    window.location.href = $find("RadNotification1").get_value();

    4.2) The user clicks the button - in this case we call update() to cause a callback and we hide the notification:

    notification.update();
    notification.hide();

    All the other code is for the demo and the timers are for the labels.

    I hope that my reply is detailed enough and helpful and for your convenience I modified and attached your code, let me know if you have additional questions.
     

    All the best,
    Svetlina Anati
    the Telerik team
    If you want to get updates on new releases, tips and tricks and sneak peeks at our product labs directly from the developers working on the RadControls for ASP.NET AJAX, subscribe to their blog feed now

  3. DevCraft Release
  4. Tawnya
    Tawnya avatar
    6 posts
    Member since:
    Nov 2011

    Posted 03 Jan 2012 Link to this post

    Thank you for the detailed response. 

    As far as the different notification ID's, at one time they were all the same but in the process of changing multiple things to try and get the control to work I think I may have missed renaming it before posting my code.

    I'm now trying an alternate solution because I struggled with the control for several days and needed to move on. I can say, the only thing I got to work was the notification displaying and then the session extending if the user clicked the button. However, I never could get the page to redirect to the control's value.

    If what I'm doing doesn't work now, I will take another stab at the rad control to see if it will work for me.

  5. Svetlina Anati
    Admin
    Svetlina Anati avatar
    2795 posts
    Member since:
    Sep 2012

    Posted 03 Jan 2012 Link to this post

    Hello Tawnya,

    Did you try the attached code? It is based on yours, it should not be hard to directly use it with small adjustments and it works fine. Please, try to incorporate it in your application and let us know how it goes. 

    All the best,
    Svetlina Anati
    the Telerik team
    If you want to get updates on new releases, tips and tricks and sneak peeks at our product labs directly from the developers working on the RadControls for ASP.NET AJAX, subscribe to their blog feed now

  6. Tawnya
    Tawnya avatar
    6 posts
    Member since:
    Nov 2011

    Posted 03 Jan 2012 Link to this post

    I'll try it now. The legacy app I'm working on is in vb, but it won't be difficult to modify. I'll let you know what results I get.

  7. Tawnya
    Tawnya avatar
    6 posts
    Member since:
    Nov 2011

    Posted 03 Jan 2012 Link to this post

    Ok - I've tried it and have only gotten it to work to the point I described earlier. In this particular app, the content pages have nested master pages. Within the secondary master page, I have a contentplaceholder that serves as a place on the content page to insert javascript, etc. This is where I have placed the javascript you sent, with no modifications.

    <%@ Page Language="VB" MasterPageFile="~/hrMaster.master" AutoEventWireup="false" MaintainScrollPositionOnPostback="true"
    CodeFile="Edit.aspx.vb" Inherits="HR_Employees_Edit" title="Edit Employee" ValidateRequest="false" %>
     
    <%@ Register assembly="AjaxControlToolkit" namespace="AjaxControlToolkit" tagprefix="cc1" %>
    <%@ Register assembly="FreeTextBox" namespace="FreeTextBoxControls" tagprefix="FTB" %>
    <asp:Content ID="header" ContentPlaceHolderID="cphInnerHead" runat="server">
       <script type="text/javascript">
     
     
     
     
           //all the timers declared below are used for the demo UI only - to update UI texts.
           //The functionality related to the scenario itself is handled by RadNotification automatically out of the box
           var mainLblCounter = null;
           var timeLeftCounter = null;
           var seconds = 60;
           var secondsBeforeShow = 60;
           var mainLabel;
     
           //start the main label counter when the page loads
           function pageLoad() {
               var xmlPanel = $find("<%= rnSessionTimeOut.ClientID %>")._xmlPanel;
               xmlPanel.set_enableClientScriptEvaluation(true);
               mainLabel = $get("mainLbl"); resetTimer("mainLblCounter", updateMainLabel, 1000);
           };
     
           //stop timers for UI
           function stopTimer(timer) {
               clearInterval(this[timer]);
               this[timer] = null;
           };
     
           //reset timers for UI
           function resetTimer(timer, func, interval) {
               this.stopTimer(timer);
               this[timer] = setInterval(Function.createDelegate(this, func), interval);
           };
     
           function OnClientShowing(sender, args) {
               //deal with UI labels
               mainLabel.innerHTML = 0;
               resetTimer("timeLeftCounter", UpdateTimeLabel, 1000);
               stopTimer("mainLblCounter");
           }
     
           function updateMainLabel(toReset) {
               secondsBeforeShow = (toReset == true) ? 60 : secondsBeforeShow - 1;
               mainLabel.innerHTML = secondsBeforeShow;
           }
     
           function OnClientHidden() {
               updateMainLabel(true);
               mainLabel.style.display = "";
               resetTimer("mainLblCounter", updateMainLabel, 1000);
           }
     
     
           //update the text in the label in RadNotification
     
           //this could also be done automatically by using UpdateInterval. However, this will cause callbacks [which is the second best solution than javascript] on every second that is being count
     
           function UpdateTimeLabel(toReset) {
     
     
     
               if (seconds == 0) {
     
                   stopTimer("timeLeftCounter");
     
                   //redirect to session expired page - simply take the url which RadNotification sent from the server to the client as value
     
                   window.location.href = $find("rnSessionTimeOut").get_value();
     
               }
     
               else {
     
                   var timeLbl = $get("timeLbl");
     
                   timeLbl.innerHTML = seconds--;
     
               }
     
           }
     
     
     
           function ContinueSession() {
     
     
     
               var notification = $find("rnSessionTimeOut");
     
               //we need to contact the server to restart the Session - the fastest way is via callback
     
               //calling update() automatically performs the callback, no need for any additional code or control
     
               notification.update();
     
               notification.hide();
     
     
     
               //resets the showInterval for the scenario where the Notification is not disposed (e.g. an AJAX request is made)
     
               //You need to inject a call to the ContinueSession() function from the code behind in such a request
     
               var showIntervalStorage = notification.get_showInterval(); //store the original value
     
               notification.set_showInterval(0); //change the timer to avoid untimely showing, 0 disables automatic showing
     
               notification.set_showInterval(showIntervalStorage); //sets back the original interval which will start counting from its full value again
     
     
     
               stopTimer("timeLeftCounter");
     
               seconds = 60;
     
               updateMainLabel(true);
     
           }
      
      
      
        </script>
    </asp:Content>
    <asp:Content ID="Content2" ContentPlaceHolderID="cphPageName" runat="server">
        <h1>EDIT EMPLOYEE</h1>
    </asp:Content>
     
    <asp:Content ID="Content1" ContentPlaceHolderID="cphHR" Runat="Server">
        
         <div class="clockSession">
            <div class="contSession">
                <div class="sesseionExpire">
                    Your Session will expire in
                    <%= Session.Timeout %>
                    minutes</div>
                <div class="showNotification">
                    Notification will be shown in:</div>
                <div class="timeRemain">
                    <span class="timeSeconds"><span id="mainLbl">60 </span></span>seconds</div>
            </div>
        </div>
        <telerik:RadNotification ID="rnSessionTimeOut" runat="server" Skin="Black" AutoCloseDelay="60000"
            Title="Continue Your Session" TitleIcon="" EnableRoundedCorners="true" Value="Search.aspx"
            OnCallbackUpdate="OnCallbackUpdate" Position="Center" OnClientShowing="OnClientShowing"
            OnClientHidden="OnClientHidden">
            <ContentTemplate>
                <div class="infoIcon">
                    <img id="Img1" src="~/images/infoIcon.png" runat="server" alt="info icon" /></div>
                <div class="notificationContent">
                    Time remaining: <span id="timeLbl">60</span>
                    <asp:Button ID="btnContinue" runat="server" Text="Continue Session" OnClientClick="ContinueSession(); return false;" />
                </div>
            </ContentTemplate>
        </telerik:RadNotification>
        <div id="SuccessView" runat="server" style="display:none;" >
                This record successfully updated!<br />
                 
                 
                 
        </div>
         
        <div id="FormView" runat="server">
    <asp:ScriptManager ID="ScriptManager1" runat="server"></asp:ScriptManager>
            Form controls here.......
    </asp:Content>

    In the codebehind, I have this in the page load:

            If Not Page.IsPostBack Then
                Session.Timeout = 2
     
                rnSessionTimeOut.ShowInterval = (Session.Timeout - 1) * 60000
     
                rnSessionTimeOut.Value = Page.ResolveClientUrl("Search.aspx")
    End If

    And this is the OnCallBackUpdate:

    Sub OnCallBackUpdate(ByVal sender As Object, ByVal e As RadNotificationEventArgs)
     
    End Sub


    The notification displays and the button, when clicked, refreshes the session. However, it will not redirect the page to the value set in the notification control.




  8. Tawnya
    Tawnya avatar
    6 posts
    Member since:
    Nov 2011

    Posted 03 Jan 2012 Link to this post

    I made the following changes to the Javascript and it worked.

    First, instead of referencing the value of the radnotification, I hard coded the window.location.href to the page I wanted to redirect it to. 

    It still didn't work.

    Then I changed the if statement from if (seconds == 0) to if (seconds < 0)

    And then it worked.

    function UpdateTimeLabel(toReset) {
     
     
     
        if (seconds < 0) {
     
            stopTimer("timeLeftCounter");
            
            //redirect to session expired page - simply take the url which RadNotification sent from the server to the client as value
     
            window.location.href = "Search.aspx";
            
     
        }
     
        else {
     
            var timeLbl = $get("timeLbl");
     
            timeLbl.innerHTML = seconds--;
     
        }
     
    }

  9. Manish
    Manish avatar
    10 posts
    Member since:
    Oct 2012

    Posted 29 Mar 2013 Link to this post

    Can you please send me a sample what if the session Timer is for 5 min.

    so i want something like-  it saying - 4 min 59 sec left ( with time decrementing)
    and when time reaches 1min 00 sec, i get the radnotification saying you have 60 secnds left, reset session and resetting session make the timer back to 5 min

    i wont confuse you with various option i tried, please provide a sample

Back to Top
DevCraft Release