Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Auto refresh ASP.NET web page after defined interval?

Tags:

asp.net

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.

like image 714
Hemant Kothiyal Avatar asked Feb 13 '26 23:02

Hemant Kothiyal


1 Answers

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

like image 65
Aristos Avatar answered Feb 15 '26 12:02

Aristos



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!