locked
Session Countdown Time in Browsers Status Bar? RRS feed

  • Question

  • User-1924666908 posted

    I want to show countdown time in the status bar of browser like:

    You session will timeout in 40:00 minutes.

    You session will timeout in 39:99 minutes. 

    I mean it should countdown to 0 and popup a window if user click Popup's Ok button then it should show a redirected page.

    No ajax support required please. I want manually coding therefore I am not allowed to write anything in web.config also.

    Any help in this regard would be appreciated.

    Sunday, March 7, 2010 1:34 AM

Answers

  • User1414178881 posted

    <script type="text/javascript">
            var allowRedirect = false;
            window.onunload = function() { if (!allowRedirect) { location.replace(this.location.href); } };
        </script>
    <asp:LinkButton ID="LinkButton1" runat="server" OnClientClick="allowRedirect=true;location.replace('Default.aspx');return false;">here</asp:LinkButton>



    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Thursday, April 8, 2010 5:54 AM
  • User1414178881 posted

    that's not a problem,in onclientclick of the login button,add this: allowRedirect=true

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Sunday, April 11, 2010 3:18 AM
  • User-1924666908 posted

    that's not a problem,in onclientclick of the login button,add this: allowRedirect=true

     

    Thankyou very much for your committed support.

    Now finally I found the solution as per my requirement.

    The final coding which may helpful to others is as follows:

    Global.asax

    Sub Session_Start(ByVal sender As Object, ByVal e As EventArgs)
            Session.Timeout = 40
             
    End Sub


    Site.master

    <%--Code for SessionTimeout CountDown--%>
    <script type="text/javascript">   
            var timeout = '<%= Session.Timeout * 60 * 1000 %>';        
            var timer = setInterval(function()
            {
               timeout -= 1000;
               window.status = "Your Session will timeout in " + time(timeout) + " minutes.";
                if (timeout == 0)
                 {
                 clearInterval(timer);
                 alert('Your Session has expired. You will be redirected to another page.!');
                 window.location.replace("SessionLogoutPage.aspx");
                  }
            }, 1000);   
             
               function two(x)
               {
                return ((x > 9) ? "" : "0") + x
               }   
      
               function time(ms)
               {   
                   var t = '';   
                   var sec = Math.floor(ms / 1000);   
                   ms = ms % 1000   
      
                   var min = Math.floor(sec / 60);   
                   sec = sec % 60;   
                   t = two(sec);   
      
                   var hr = Math.floor(min / 60);   
                   min = min % 60;   
                   t = two(min) + ":" + t;   
                return t;           
               }
    </script>


    SessionLogoutPage.aspx

    <script type="text/javascript">   
       var allowRedirect = false;
       window.onunload = function() {if(!allowRedirect){location.replace(this.location.href);} };
    </script>
    

    and put this linkbutton code in the form tag in Site.master:

    <asp:LinkButton ID="LinkButton1" runat="server" OnClientClick="allowRedirect=true;location.replace('LoginPage.aspx');return false;">here</asp:LinkButton>
    


    LoginPage.aspx

    <script type="text/javascript">   
            var allowRedirect = false;   
            window.onunload = function() { if (!allowRedirect) { location.replace(this.location.href); } };   
    </script> 

     

     

    As the problem has solved, by the way if I want to disable back button of website in all pages and want to handle by having back button in each page then how it is possible?

    As the this thread is almost very very long and the problem solved therefore I need your guidance if you'll reply to my another thread for the mentioned problem:

    http://forums.asp.net/p/1544494/3778724.aspx#3778724

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Sunday, April 11, 2010 4:00 AM

All replies

  • User1414178881 posted

    But session could expire before it's time for many possible reasons; memory leak in the server,worker process recycled,unhandled application error,...etc,also session always reset it's expire in postbacks.

    I just wanted to clear things for you that's all.

    Now regarding to what you asked you have to use JavaScript,take a look at this sample:

     <script type="text/javascript">
                var timeout = '<%= Session.Timeout * 60 * 1000 %>';
                var timer = setInterval(function() { timeout -= 1000; window.status = time(timeout); if (timeout == 0) { clearInterval(timer); alert('Times up PAL!') } }, 1000);
    
                function two(x) { return ((x > 9) ? "" : "0") + x }
    
                function time(ms) {
                    var t = '';
                    var sec = Math.floor(ms / 1000);
                    ms = ms % 1000
    
                    var min = Math.floor(sec / 60);
                    sec = sec % 60;
                    t = two(sec);
    
                    var hr = Math.floor(min / 60);
                    min = min % 60;
                    t = two(min) + ":" + t;
                    
                    return "You session will timeout in " + t + " minutes.";
                }
            </script>


    NOTE: IE is the only browser that enable writing in it's statusbar,in FireFox you have to change it's configuration,but I think that other browsers don't support it (like Google Chrome,Safari,..etc)


    Sunday, March 7, 2010 4:03 AM
  • User665034424 posted

    Instead of trying to use the status bar, you can add a label to your page and use javascript to display it as a counter.

    http://www.javascriptsearch.com/scripts/Counters/simple_counter.html

    Sunday, March 7, 2010 4:12 AM
  • User-1924666908 posted

    Instead of trying to use the status bar, you can add a label to your page and use javascript to display it as a counter.

    http://www.javascriptsearch.com/scripts/Counters/simple_counter.html

     

    Thanks for reply, but the link you provided is for html pages only and there is no involvement of Sessions.

    Sunday, March 7, 2010 4:37 AM
  • User1414178881 posted

    What about the code I posted earlier? It has an involvement of Sessions!

    Sunday, March 7, 2010 4:55 AM
  • User-1924666908 posted

    But session could expire before it's time for many possible reasons; memory leak in the server,worker process recycled,unhandled application error,...etc,also session always reset it's expire in postbacks.

    I just wanted to clear things for you that's all.

    Now regarding to what you asked you have to use JavaScript,take a look at this sample:

     <script type="text/javascript">
                var timeout = '<%= Session.Timeout * 60 * 1000 %>';
                var timer = setInterval(function() { timeout -= 1000; window.status = time(timeout); if (timeout == 0) { clearInterval(timer); alert('Times up PAL!') } }, 1000);
    
                function two(x) { return ((x > 9) ? "" : "0") + x }
    
                function time(ms) {
                    var t = '';
                    var sec = Math.floor(ms / 1000);
                    ms = ms % 1000
    
                    var min = Math.floor(sec / 60);
                    sec = sec % 60;
                    t = two(sec);
    
                    var hr = Math.floor(min / 60);
                    min = min % 60;
                    t = two(min) + ":" + t;
                    
                    return "You session will timeout in " + t + " minutes.";
                }
            </script>


     

    NOTE: IE is the only browser that enable writing in it's statusbar,in FireFox you have to change it's configuration,but I think that other browsers don't support it (like Google Chrome,Safari,..etc)


     

    Sorry for replying late but how to show the time in browser? Should I take a label? If yes then how to show above coding time in the label?

    Looking for your reply please.

    Monday, April 5, 2010 2:53 AM
  • User1414178881 posted

    Yes you can take a label,it's better that way.

    mmm...use a span instead of serverside label.

    add this span at the top of your page for instance:

    <span id="countDown"></span>

    then replace this statment:

    window.status = time(timeout);

    with this:

    document.getElementById('countDown').innerHTML = time(timeout);


    Monday, April 5, 2010 3:06 AM
  • User-1924666908 posted

    Yes you can take a label,it's better that way.

    mmm...use a span instead of serverside label.

    add this span at the top of your page for instance:

    <span id="countDown"></span>

    then replace this statment:

    window.status = time(timeout);

    with this:

    document.getElementById('countDown').innerHTML = time(timeout);

     

    Ok, but how to use lable? How to assign time to label? 

    Monday, April 5, 2010 3:28 AM
  • User1414178881 posted

    Easy,instead of span just use label:

    <asp:Label id="countDown" runat="server"></Label>

    but you have to use this statement:

    document.getElementById('<%=countDown.ClientID %>').innerHTML = time(timeout);

    Monday, April 5, 2010 3:32 AM
  • User-1924666908 posted

    Easy,instead of span just use label:

    <asp:Label id="countDown" runat="server"></Label>

    but you have to use this statement:

    document.getElementById('<%=countDown.ClientID %>').innerHTML = time(timeout);

     

    Ok, it is working but some little tunning required.

    1. On popup when I click Ok it should redirect to LoginPage.aspx

    2. Is it possible that the countDown display in the status bar?

     

    Monday, April 5, 2010 3:49 AM
  • User1414178881 posted

    1. On popup when I click Ok it should redirect to LoginPage.aspx

    that's easy,after the statement: alert('Times up PAL!');

    add this: window.location.href='LoginPage.aspx';

    2. Is it possible that the countDown display in the status bar?

    As I said earlier,this feature is supported ONYL in IE,it's not supported in any other browsers,if you are going to build a cross browser website then you have to forget about displaying the countDown in the status bar.

    BUT,you can create your bar a the bottom of your page (using a div with position static,bottom 0px),displaying in it the countDown...it's an option.

    Monday, April 5, 2010 3:57 AM
  • User-1924666908 posted

    As I said earlier,this feature is supported ONYL in IE,it's not supported in any other browsers,if you are going to build a cross browser website then you have to forget about displaying the countDown in the status bar.

    Yes I know this. In our enviornment no one allowed to use browsers except IE. So for this specific purpose it is fine.

    that's easy,after the statement: alert('Times up PAL!');

    add this: window.location.href='LoginPage.aspx';

    I did as per your suggestions but there is a little problem, on session expired when I get LoginPage.aspx I clicked browsers back button then I moved to the previous page and all links on that page worked as normal. It means no session expired? 

    BUT,you can create your bar a the bottom of your page (using a div with position static,bottom 0px),displaying in it the countDown...it's an option.

     

    After solving first issue i'll implement it later.

     

    Monday, April 5, 2010 4:16 AM
  • User1414178881 posted

    Yes I know this. In our enviornment no one allowed to use browsers except IE. So for this specific purpose it is fine.

    my earlier code :  window.status = time(timeout);  will do it.

    there is a little problem, on session expired when I get LoginPage.aspx I clicked browsers back button then I moved to the previous page and all links on that page worked as normal. It means no session expired? 

    That's a cache problem,you should block the page from getting cached,this is the first thing.

    add this line at page_load of the page the has the countDown:

    Response.Cache.SetCacheability(HttpCacheability.NoCache)

    second,when the user login to your website you should have stored a flag into your session,something like Session["logged"] = true;

    in page_load of the page (where it has a count down) you should check for the availability of the session,Session["logged"] will be null if session expired and in that case you have to redirect the user to the login page (this method with combination of disabling the cache will block the user from returning back.)

    if (Session["logged"] == null)

    {

    Response.Redirect("LoginPage.aspx");

    }


    Monday, April 5, 2010 4:31 AM
  • User-1924666908 posted

     

    I have already implemented following code in the masterpage:

    Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
    
            '********* Redirection on Session expiry.**********
            If Session("UserName") = Nothing Then
                'Page.ClientScript.RegisterStartupScript([GetType](), "alert", "alert('Your session has expired. Pleas Login again.')", True)
                Response.Redirect("SessionLogoutPage.aspx")
    
            End If
    End Sub
    


     


    In Global.asax:

    Sub Session_Start(ByVal sender As Object, ByVal e As EventArgs)
            Session.Timeout =40
             
    End Sub


    I need your assistance in my code please.

     

    Monday, April 5, 2010 4:42 AM
  • User1414178881 posted

    Perfect,this code is good but you have to disable caching on your page too.

    Note: the statement I provided earlier to disable caching can be added in page_load of your master page but this will disable caching on any content page of it,if that's not a problem then do it.

    Monday, April 5, 2010 4:53 AM
  • User-1924666908 posted

    Perfect,this code is good but you have to disable caching on your page too.

    Note: the statement I provided earlier to disable caching can be added in page_load of your master page but this will disable caching on any content page of it,if that's not a problem then do it.

     

    Now I have implemented following code snippet but all is same, means if I click back button of browser then the timer restarts and all links on the page working as normal:

    Masterpage Page Load event:

    If Session("UserName") = Nothing Then
                'First two lines are Ok
                'Page.ClientScript.RegisterStartupScript([GetType](), "alert", "alert('Your session has expired. Pleas Login again.')", True)
                Response.Cache.SetCacheability(HttpCacheability.NoCache)
                Response.Redirect("SessionLogoutPage.aspx")
    
                'Page.ClientScript.RegisterStartupScript(Me.GetType(), "MyScript", "alert('Your session expired. Please Login again!'); window.location = 'LoginPage.aspx'", True)
                'Page.ClientScript.RegisterStartupScript(Me.GetType(), "MyScript", "alert('Your session expired. Please Login again!');", True)
    End If


     

    Monday, April 5, 2010 5:15 AM
  • User1414178881 posted

    The statement Response.Cache.SetCacheability(HttpCacheability.NoCache)  must be executed all the time,which means it must be OUTSIDE it's current block,move the statement to be after or before (If Session("UserName") = Nothing Then  )

    Monday, April 5, 2010 5:30 AM
  • User-1924666908 posted

    The statement Response.Cache.SetCacheability(HttpCacheability.NoCache)  must be executed all the time,which means it must be OUTSIDE it's current block,move the statement to be after or before (If Session("UserName") = Nothing Then  )

     

    Ok now it is working but regarding status bar, I have tried your option but it is not going to status bar.

    The code which I implemented in Master page to put countDown to show in status bar is:

    <div style="width: 1208px; height: 16px" position = "static" bottom ="0px">
         <table style="width: 1205px">
           <tr>
             <td style="width: 129px">
               <asp:Label ID="lblSuggestions" runat="server" Font-Names="Verdana" Font-Size="X-Small" ForeColor="White" Height="15px" Style="" Text="Comments & Suggestions to: hsumar@etisalat.ae" Width="287px" Font-Bold="False"></asp:Label></td>
             <td style="width: 280px; text-align: center">
               <asp:Label ID="lblES" runat="server" Font-Bold="False" Font-Names="Verdana" Font-Size="Small" ForeColor="YellowGreen" Height="15px" Style="" Text="Engineering Support (AUH)"  Width="210px"></asp:Label></td>
             <td style="width: 100px; text-align: right">
               <asp:Label ID="countDown" runat="server" Width="306px" ForeColor="White" Font-Names="Verdana" Font-Size="X-Small"></asp:Label></td>
           </tr>
          </table>
    </div>


     

    Monday, April 5, 2010 5:56 AM
  • User1414178881 posted

    my mistake Tongue out,I meant position absolute

    Monday, April 5, 2010 6:10 AM
  • User-1924666908 posted

    my mistake Tongue out,I meant position absolute

     

    But how it will go to status bar of the browser window?

    I have implemented position absolute but all is same.

    Monday, April 5, 2010 6:17 AM
  • User1414178881 posted

    It will not go to status bar,it will go to the bottom of your page (above the statusbar),it's an alternative option if you want your website to work on all browsers (as I said earlier,IE alone support writing into it's status bar),you said you don't have a problem to write into IE's statusbar because you want to support IE alone and I suggested to use the old statement: window.status =...,correct?

    Monday, April 5, 2010 6:26 AM
  • User-1924666908 posted

    Response.Cache.SetCacheability(HttpCacheability.NoCache)

     

    Oh no.

    By applying above line of code I found one problem, if user click one page then he redirects to other and by clicking on the bowsers back button it is displaying that the session expired.

    Whats happening?

    If I un-comment above line of code then it is going back but on session expiry and if I click back button then session restarts.

     

    Monday, April 5, 2010 6:33 AM
  • User1414178881 posted

    By applying above line of code I found one problem, if user click one page then he redirects to other and by clicking on the bowsers back button it is displaying that the session expired.

    You mean web page expired,it means the cached page is not exists (we disabled caching using above line),which in that case the user will have to refresh the page by clicking on the refresh button (it's something by design in IE),leading to calling page_load that eventually will redirect him/her to the login page.

    If that page ("web page expired") is not desired then you have to block the user from returning back (using javascript),put this code in the header of your masterpage:

    <script type="text/javascript">
    window.history.forward(1);
    </script>

    Monday, April 5, 2010 6:49 AM
  • User-1924666908 posted

    You mean web page expired,it means the cached page is not exists (we disabled caching using above line),which in that case the user will have to refresh the page by clicking on the refresh button (it's something by design in IE),leading to calling page_load that eventually will redirect him/her to the login page.

    If that page ("web page expired") is not desired then you have to block the user from returning back (using javascript),put this code in the header of your masterpage: 

    <script type="text/javascript">
    window.history.forward(1);
    </script>

     

     

    I put above code in masterpage, on session expiry it is ok not going back by clicking on browser's back button but I want to let you know that in my website some pages are dependent on the back button because if i click some link so redirects to next page and there is no option to return on the parent page without clicking on Back button of browser.

    So what to do?

    I am very thankful to you for your time that you are replying quickly but I need some fine tuning to close this thread.

    Because I have implemented your provided code therefore I don't want to try another codes will take time and I have to start from the scratch so I want your help to finish this task.

    Please reply.

     

    Tuesday, April 6, 2010 3:49 AM
  • User1414178881 posted

    So what to do?

    That's not a problem,any page that needs to have the back functionality you must implements a back button (it's just a simple button that has a code like: Response.Redirect("somepage.aspx"))

    Tuesday, April 6, 2010 3:54 AM
  • User-1924666908 posted

    So what to do?

    That's not a problem,any page that needs to have the back functionality you must implements a back button (it's just a simple button that has a code like: Response.Redirect("somepage.aspx"))

     

    But the problem is that if I click on some value in the gridview then it redirects to other page and the new page also contains another gridview with some values and if I click some gridview value then redirects to some other page.

    From the start I am facing much difficulty to remember the id of the link clicked therefore i am getting this problem.

    If I'll use Response.Redirect("somepage.aspx") then definately it will redirect but page will appears from the start not open in the last stage.

     

    Tuesday, April 6, 2010 4:01 AM
  • User1414178881 posted

    mmm...I want to ask you a question,does the countDown used in the whole website or just in one page? because if it's used in one page then you have to move the JS script to that page only,also you will have to move that line:Response.Cache.SetCacheability(HttpCacheability.NoCache) from the masterpage to your page.

    In other words,you will have to disable cache and back button only on that page.

    Tuesday, April 6, 2010 4:18 AM
  • User-1924666908 posted

    mmm...I want to ask you a question,does the countDown used in the whole website or just in one page? because if it's used in one page then you have to move the JS script to that page only,also you will have to move that line:Response.Cache.SetCacheability(HttpCacheability.NoCache) from the masterpage to your page.

    In other words,you will have to disable cache and back button only on that page.

     

    Session is for Login and Logout for the website therefore it is showing in all pages.

    And now I have assigned to the browser window status with the help of following code:

    window.status = "Your session will timeout in " + t + " minutes.";

    before 

    return "Your session will timeout in " + t + " minutes.";

    Tuesday, April 6, 2010 4:36 AM
  • User1414178881 posted

    Let's reformat my question.Does that script added into the masterpage or to one of your pages?and Is it needed to appear into all pages?

    Tuesday, April 6, 2010 4:57 AM
  • User-1924666908 posted

    Let's reformat my question.Does that script added into the masterpage or to one of your pages?and Is it needed to appear into all pages?

     

    The script for countdown you provided is implemented in masterpage thats why I am getting countdown in the window in all pages.

    Following script is implemented in the SessionLogoutPage.aspx:

    <script type="text/javascript">   
    window.history.forward(1);   
    </script> 


     

    Tuesday, April 6, 2010 6:14 AM
  • User1414178881 posted

    A question : Why do you want to use the session countDown in the first place? it will reset every time you do a postback so I'm asking you to tell me why do you want it?what are you trying to achieve?

    At first I thought you are trying to implement some kind of a online quize or something.


    Tuesday, April 6, 2010 6:42 AM
  • User-1924666908 posted

    A question : Why do you want to use the session countDown in the first place? it will reset every time you do a postback so I'm asking you to tell me why do you want it?what are you trying to achieve?

    At first I thought you are trying to implement some kind of a online quize or something.


     

    Sorry to bother you.

    I think I may not discribe what I want!

    Actually I want to that the when user login, his session start and if he is not using on the website then after alloted time the session will expire.

    This is not online quiz. This is just intranet website.

    Hope it is understandable.

    Tuesday, April 6, 2010 6:58 AM
  • User1414178881 posted

    You are not bothering me at all.

    I just want to tell you that the timer will reset when you postback or navigate between the pages,if that's not a problem then you can remove disable chaching.

    Tuesday, April 6, 2010 7:13 AM
  • User1414178881 posted

    Test 123

    Tuesday, April 6, 2010 8:41 AM
  • User-1924666908 posted

    Test 123

     

    Actually I am not in office, when I implement it let you know.

    Tuesday, April 6, 2010 8:57 AM
  • User-1924666908 posted

    You are not bothering me at all.

    I just want to tell you that the timer will reset when you postback or navigate between the pages,if that's not a problem then you can remove disable chaching.

     

    Yes this is the case when user postback the time should restart because it is a session. If there is no activity then countdown to 0 and if user click OK then it should redirected to "SessionLogoutPage.aspx" and if user click back button of the browser then it should not go back but Back button of browser should work when the Session is active.

    I removed Caching but back button on SessionLogoutPage.aspx is still working and if I click, it goes back to its previous page with the new session starts.

    Finally I have displayed countdown in the window.status.

    Now this is my final coding which I implemented. Pleas have a look again where I am lacking.:

    Global.asax:

    Sub Session_Start(ByVal sender As Object, ByVal e As EventArgs)
            Session.Timeout = 40
             
    End Sub
    


     

    MasterPage.master.vb:

    Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
    
            'Response.Cache.SetCacheability(HttpCacheability.NoCache)
            '********* Redirection on Session expiry.**********
            If Session("UserName") = Nothing Then
                Response.Redirect("SessionLogoutPage.aspx")
            End If
     End Sub
    


     

    MasterPage.master:

    <script type="text/javascript">   
    
            var timeout = '<%= Session.Timeout * 60 * 1000 %>';           
            var timer = setInterval(function()
            {
               timeout -= 1000;
               window.status = "Your Session will timeout in " + time(timeout) + " minutes.";
                if (timeout == 0)
                 {
                 clearInterval(timer);
                 alert('Your Session has expired. You will be redirected to another page.!');
                 window.location.href='SessionLogoutPage.aspx';
                  }
            }, 1000);   
             
               function two(x)
               {
                return ((x > 9) ? "" : "0") + x
               }   
      
               function time(ms)
               {   
                   var t = '';   
                   var sec = Math.floor(ms / 1000);   
                   ms = ms % 1000   
      
                   var min = Math.floor(sec / 60);   
                   sec = sec % 60;   
                   t = two(sec);   
      
                   var hr = Math.floor(min / 60);   
                   min = min % 60;   
                   t = two(min) + ":" + t;       
                   return t;      
               }   
    </script>  


    SessionLogoutPage.aspx:

    <script type="text/javascript">   
    window.history.forward(1);   
    </script> 


     

    Hope you'll reply soon.

    Tuesday, April 6, 2010 11:26 PM
  • User-1924666908 posted

    Test 123

     

    Please reply. I achieved nothing today.

    Wednesday, April 7, 2010 6:03 AM
  • User1414178881 posted

    Hi,

    Sorry for late responding,I was in a long meeting.

    here is the solution,instead of using:

    window.location.href='SessionLogoutPage.aspx';

    use this:

    window.location.replace('SessionLogoutPage.aspx');

    and remove this line:

    window.history.forward(1);

    Wednesday, April 7, 2010 8:46 AM
  • User-1924666908 posted

    Hi,

    Sorry for late responding,I was in a long meeting.

    here is the solution,instead of using:

    window.location.href='SessionLogoutPage.aspx';

    use this:

    window.location.replace('SessionLogoutPage.aspx');

    and remove this line:

    window.history.forward(1);

     

    I did same as per your suggestions but on session expiry when I click ok and moved to SessionLogoutPage.aspx and when I am clicking Back button of browser then still going back and session starts.

    Thursday, April 8, 2010 3:22 AM
  • User-1924666908 posted

     

    One more thing I want to share that I think that the code of Page_Load of Master page is not working because I have changed

    Response.Redirect("....") to some other page then it is not redirecting to other page.

    So the question is that how session will work?

    The page I am getting on session expiry fRom:

    window.location.replace('SessionLogoutPage.aspx');

     

    Thursday, April 8, 2010 3:30 AM
  • User1414178881 posted

    I did same as per your suggestions but on session expiry when I click ok and moved to SessionLogoutPage.aspx and when I am clicking Back button of browser then still going back and session starts.

    What?! back button shouldn't work at all,once you redirected to logout page,you will not be able to return back to the earlier page at all because we replaced it's history with the logout page.

    Unless you mean you can go back i.e.two pages.

    It seems to me you don't want the user to go back at all after session is expired,which means that you have to block back button but you said you want to be able to go back as long as session is not expired,in this case we have to disable caching but you don't want the user to see "WebPage expired" page.

    I'm running out of options,I can't think of a solution that will work for all of these cases,unless....how about not allowing the user to not leave the logout page at all,add this into the logout page:

    <script type="text/javascript">
        window.onunload = function() { location.replace(this.location.href) };
    </script>




    Thursday, April 8, 2010 4:18 AM
  • User-1924666908 posted

    I did same as per your suggestions but on session expiry when I click ok and moved to SessionLogoutPage.aspx and when I am clicking Back button of browser then still going back and session starts.

    What?! back button shouldn't work at all,once you redirected to logout page,you will not be able to return back to the earlier page at all because we replaced it's history with the logout page.

    Unless you mean you can go back i.e.two pages.

    It seems to me you don't want the user to go back at all after session is expired,which means that you have to block back button but you said you want to be able to go back as long as session is not expired,in this case we have to disable caching but you don't want the user to see "WebPage expired" page.

    I'm running out of options,I can't think of a solution that will work for all of these cases,unless....how about not allowing the user to not leave the logout page at all,add this into the logout page:

    <script type="text/javascript">
        window.onunload = function() { location.replace(this.location.href) };
    </script>



     

     

    Thanks now it is working but on SessionLogoutPage.aspx there is a link:

    Click HERE to login again.

    When I clicked on HERE it is not redirecting to login page.

    Thursday, April 8, 2010 4:41 AM
  • User-1924666908 posted

     

    Hello test 123........

    Thursday, April 8, 2010 5:36 AM
  • User1414178881 posted

    <script type="text/javascript">
            var allowRedirect = false;
            window.onunload = function() { if (!allowRedirect) { location.replace(this.location.href); } };
        </script>
    <asp:LinkButton ID="LinkButton1" runat="server" OnClientClick="allowRedirect=true;location.replace('Default.aspx');return false;">here</asp:LinkButton>



    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Thursday, April 8, 2010 5:54 AM
  • User-1924666908 posted

     

    <script type="text/javascript">
            var allowRedirect = false;
            window.onunload = function() { if (!allowRedirect) { location.replace(this.location.href); } };
        </script>
    <asp:LinkButton ID="LinkButton1" runat="server" OnClientClick="allowRedirect=true;location.replace('Default.aspx');return false;">here</asp:LinkButton>

     

     

    Thanks for your kind support but now the problem is with the Default.aspx page.

    When user tries to go back then in this situation it is allowed and he can go back and session restarts.

    Thursday, April 8, 2010 6:46 AM
  • User-1924666908 posted

     

    <script type="text/javascript">
            var allowRedirect = false;
            window.onunload = function() { if (!allowRedirect) { location.replace(this.location.href); } };
        </script>
    <asp:LinkButton ID="LinkButton1" runat="server" OnClientClick="allowRedirect=true;location.replace('Default.aspx');return false;">here</asp:LinkButton>



     

     

    Your reply needed please.

    Sunday, April 11, 2010 2:28 AM
  • User1414178881 posted

    location.replace('Default.aspx'); is just an example,default.aspx must be replaced with the url to login page.

    Also you have to add the same code into login page:

    <script type="text/javascript">
            var allowRedirect = false;
            window.onunload = function() { if (!allowRedirect) { location.replace(this.location.href); } };
        </script>



    Sunday, April 11, 2010 2:38 AM
  • User-1924666908 posted

    location.replace('Default.aspx'); is just an example,default.aspx must be replaced with the url to login page.

    Also you have to add the same code into login page:

     

    <script type="text/javascript">
            var allowRedirect = false;
            window.onunload = function() { if (!allowRedirect) { location.replace(this.location.href); } };
        </script>

     

    Actually I already tried same you suggested.

    When I first run my project then after putting id and password then it is not loging in because of this.location.href in LoginPage.

    Sunday, April 11, 2010 3:14 AM
  • User1414178881 posted

    that's not a problem,in onclientclick of the login button,add this: allowRedirect=true

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Sunday, April 11, 2010 3:18 AM
  • User-1924666908 posted

    that's not a problem,in onclientclick of the login button,add this: allowRedirect=true

     

    Thankyou very much for your committed support.

    Now finally I found the solution as per my requirement.

    The final coding which may helpful to others is as follows:

    Global.asax

    Sub Session_Start(ByVal sender As Object, ByVal e As EventArgs)
            Session.Timeout = 40
             
    End Sub


    Site.master

    <%--Code for SessionTimeout CountDown--%>
    <script type="text/javascript">   
            var timeout = '<%= Session.Timeout * 60 * 1000 %>';        
            var timer = setInterval(function()
            {
               timeout -= 1000;
               window.status = "Your Session will timeout in " + time(timeout) + " minutes.";
                if (timeout == 0)
                 {
                 clearInterval(timer);
                 alert('Your Session has expired. You will be redirected to another page.!');
                 window.location.replace("SessionLogoutPage.aspx");
                  }
            }, 1000);   
             
               function two(x)
               {
                return ((x > 9) ? "" : "0") + x
               }   
      
               function time(ms)
               {   
                   var t = '';   
                   var sec = Math.floor(ms / 1000);   
                   ms = ms % 1000   
      
                   var min = Math.floor(sec / 60);   
                   sec = sec % 60;   
                   t = two(sec);   
      
                   var hr = Math.floor(min / 60);   
                   min = min % 60;   
                   t = two(min) + ":" + t;   
                return t;           
               }
    </script>


    SessionLogoutPage.aspx

    <script type="text/javascript">   
       var allowRedirect = false;
       window.onunload = function() {if(!allowRedirect){location.replace(this.location.href);} };
    </script>
    

    and put this linkbutton code in the form tag in Site.master:

    <asp:LinkButton ID="LinkButton1" runat="server" OnClientClick="allowRedirect=true;location.replace('LoginPage.aspx');return false;">here</asp:LinkButton>
    


    LoginPage.aspx

    <script type="text/javascript">   
            var allowRedirect = false;   
            window.onunload = function() { if (!allowRedirect) { location.replace(this.location.href); } };   
    </script> 

     

     

    As the problem has solved, by the way if I want to disable back button of website in all pages and want to handle by having back button in each page then how it is possible?

    As the this thread is almost very very long and the problem solved therefore I need your guidance if you'll reply to my another thread for the mentioned problem:

    http://forums.asp.net/p/1544494/3778724.aspx#3778724

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Sunday, April 11, 2010 4:00 AM