user69889
user69889

Reputation: 585

Refresh a web page in ASP.NET automatically and capture the time until the next refresh

I know there should be an easy way to do this but I guess my brain isn't working today. I need to refresh a page in x number of seconds. I can do that in .NET using the following:

Response.AddHeader("Refresh", "300");

This is fine except I also need to display the number of minutes and seconds until the next refresh.

So I am using the following code in javascript to do this from the client:

<script type="text/javascript"">


    //enter refresh time in "minutes:seconds" Minutes should range from 0 to inifinity. Seconds should range from 0 to 59
    var limit="0:30"

    if (document.images){
        var parselimit=limit.split(":")
        parselimit=parselimit[0]*60+parselimit[1]*1
    }

    function beginrefresh(){
        if (!document.images)
            return

        if (parselimit==1)
            window.location.reload()
        else{ 
            parselimit-=1
            curmin=Math.floor(parselimit/60)
            cursec=parselimit%60
            if (curmin!=0)
                curtime=curmin+" minutes and "+cursec+" seconds left until page refresh!"
            else
                curtime=cursec+" seconds left until page refresh!"

            window.status=curtime
            setTimeout("beginrefresh()",1000)
        }
    }

    window.onload=beginrefresh;

</script>

This works except for one thing - every time the page is refreshed, the browser (IE) gives the nag message about re-downloading content or something. This will not do. This is a page that users will have open all day and it needs to refresh without user intervention. I suppose I could do away with the message that displays the time till next refresh and just use the ASP.NET line, but if anyone has a better idea, I would really appreciate it.

Upvotes: 1

Views: 5490

Answers (3)

Stephen Wrighton
Stephen Wrighton

Reputation: 37819

the "nag" is from the browser refreshing content that has been posted to the server once, and is because of your "reload" line.

There is another option to refreshing the entire page, which is just refreshing the parts of the page you're actually changing using AJAX functionality.

Upvotes: 0

Kirtan
Kirtan

Reputation: 21695

You can write a bit of JavaScript for the refreshing purpose along with the code for refreshing the timer. To refresh a page, you'll have to write this -

window.location.href = window.location.href

This line of JS will refresh the page without the NAG screen you are talking about (The nag screen is actually a question asked by the browser, about whether you want to re-post the POST data to the server).

Upvotes: 0

Seibar
Seibar

Reputation: 70243

Instead of using window.location.reload(), try doing a redirect to the current URL:

window.location = window.location;

This way, the browser will not be doing a post back, but a new request to the server. You'll want to test this in various browsers. I just tested FF 3 & IE8 quickly; both look good.

Upvotes: 4

Related Questions