3

In one of my website, i required to implement automatic refresh of webpage after 15 minute.

For this to achive i have write following line of code

<meta http-equiv="refresh" content="60;url=" />

But i am facing one problem that after this duration of 15minute page will refresh as a new page load.

At my page i have used combo box having city list, there is a case when i select an item from this list at index 3. After that i just make page idle and after 15 minute page is refresh with the script i write for auto post back (mentioned above). But the problem is that due to this page is reload as a new page and code inside (!PostBack) execute which refill combobox and reset at index 1.

Please help me to solve this problem?

My basic requirement is that whenever user reaches that page and makes it idle for longer time, session should not expire and hence i am writing above script so that session would be live.

abatishchev
  • 98,240
  • 88
  • 296
  • 433
Hemant Kothiyal
  • 4,092
  • 19
  • 61
  • 80
  • 2
    Do you really need to refresh the entire page? Can't you just wrap whatever needs refreshing in an AJAX UpdatePanel ? – codingbadger Aug 20 '10 at 07:25
  • My requirement is that whenever user reach that page and make it Idle, then session should not expire from that page. Hence i write auto refresh script. Any other way to make session live? – Hemant Kothiyal Aug 20 '10 at 07:29
  • possible duplicate of [What is the best approach to handle session timeouts in asp.net](http://stackoverflow.com/questions/3308918/what-is-the-best-approach-to-handle-session-timeouts-in-asp-net) – Druid Mar 05 '13 at 10:30

2 Answers2

2

Here is a nice javascript trick to keep the session alive - and not refreshing the full page.

<img id="keepAliveIMG" width="1" height="1" src="/img/ui/spacer.gif?" /> 

<script language="javascript" type="text/javascript"> 
    var myImg = document.getElementById("keepAliveIMG");

    if (myImg){
        window.setInterval(function(){
              myImg.src = myImg.src.replace(/\?.*$/, '?' + Math.random());
            }, 6000);
    }   
</script>

(change the time on timer as you like - now is on 6sec)

Similar post: What is the best approach to handle session timeouts in asp.net

Community
  • 1
  • 1
Aristos
  • 66,005
  • 16
  • 114
  • 150
  • Thanks, Can you please explain me that where should i write this code inside or . How this is working? – Hemant Kothiyal Aug 20 '10 at 07:56
  • @Hemant As it is you place it on bottom of the page just before the body close. What I do here is that I reload every 6 seconds an small empty image 1x1 pixel, so server not close the session. To avoid the cache I place a random number on the end of the image. The script as it is must be after the img ! Other way is to make an onload and call the script after the page loaded. – Aristos Aug 20 '10 at 08:06
  • I just write same peice of javascript code but don't write image tag. Still it is working fine without image. How? – Hemant Kothiyal Aug 20 '10 at 08:58
  • @Hemant Write the code as you see it (especial if you do not understand what its doing) Everything else - is something that I do not know it and need to see the full page to understand it. With out find the Image tag with id=keepAliveImg, the function is not run. – Aristos Aug 20 '10 at 09:29
  • @Hemant - just copy/paste it as it is on your end of your page. Thats all. – Aristos Aug 20 '10 at 09:30
1

I would think a timer (with Viewstate turned on) would do what you're looking for -- Refresh the page but retain the current data and selections. Just make sure that on the Page_Load and Tick events, you're not re-binding any data.

Use this around to not around your binding code so it only binds the first time the page is loaded

if (!IsPostBack)
{
}

Put a Timer on your ASPX page

<asp:Timer id="Timer1" Interval="900000" />

900000 should be 15 minutes in milliseconds.

Jemes
  • 2,822
  • 21
  • 22
  • I just now noticed your comment about WHY you're looking to do the refresh. You may also want to surround the Timer in an UpdatePanel so that the refresh is not visible to the user. You can also consider just changing the Session.Timeout interval. – Jemes Aug 20 '10 at 14:17