Click here to Skip to main content
15,882,017 members
Articles / Web Development / ASP.NET

Auto Refresh Web Page

Rate me:
Please Sign up or sign in to vote.
4.50/5 (11 votes)
10 Oct 2007CPOL2 min read 274.6K   2.3K   64   20
Some simple ways to auto refresh your web page.

Screenshot - autorefresh.gif

Introduction

When writing web pages, sometimes there is a need to auto refresh a page. Honestly, AJAX is a good solution most of the time. The problem comes when you don't want to load all the AJAX stuff to do something simple.

Background

I was doing an enhancement for a download site. They wanted to display a counter of how many times a file had been downloaded. So I started to investigate ways to auto refresh a web page to show the counter changing.

The Problem

In my download example, I use two pages. One page displays all the files that can be downloaded (download.aspx). When a link is clicked, I do a server transfer to a second page which uses the Response object to deliver content and update the counter. The problem is that the server transfer keeps me from being able to refresh the counter on the download.aspx page.

The Solution

For the download example, I decided on a JavaScript solution. You can also refresh a page with a meta tag, so I will show those details as well.

Some Details on JavaScript Refresh

The JavaScript solution is pretty straightforward. On the PreRender of the Repeater, I add the JavaScript to the link button:

C#
protected void rFiles_preRender(object sender, EventArgs e)
{
 LinkButton lbtn = null;
 foreach (RepeaterItem ri in rpFiles.Items)
 {
  if (ri.ItemType == ListItemType.Item ||
      ri.ItemType == ListItemType.AlternatingItem)
  {
   lbtn = (LinkButton)ri.FindControl("lbtnFile");
   if (lbtn != null)
   { //reload the page in 1 second.
    lbtn.Attributes.Add("onclick", "setReloadTime(1)");
   }
  }
 } //foreach
}

This allows the JavaScript to run before my postback which does the Server.Transfer. The JavaScript function looks like this:

JavaScript
<script type="text/javascript" language="javascript">
 var reloadTimer = null;
 var sURL = unescape(window.location.pathname);
 
 function setReloadTime(secs) 
 { //this function is used to refresh the broswer
    if (arguments.length == 1) 
    { //if some seconds are passed in then create and set the timer and 
      //have it call this function again with no seconds passed in
        if (reloadTimer) clearTimeout(reloadTimer);
        reloadTimer = setTimeout("setReloadTime()", 
                         Math.ceil(parseFloat(secs) * 1000));
    }
    else 
    { //No seconds were passed in the timer must be up clear the timer 
     //and refresh the browser
        reloadTimer = null;
        //passing true causes the request to go back to the web server
        // false refreshs the page from history
        //This is javascript 1.2
        location.reload(true);
        //This is javascript 1.1
        window.location.replace( sURL );
    }
 }
</script>

Here are some links that I used to help me with the JavaScript:

Some Details on the Meta Tag Refresh

The meta tag Refresh has been around for a long time. Often, you see it when an old web page has moved to a new web page. You usually put the meta tag in the header of your page. The syntax is:

HTML
<meta http-equiv="Refresh" content="n;url"/>

where n is the number of seconds, and url is the URL to refresh to. If you leave the URL off, then the page refreshes itself.

In my sample code, the page updates the current time every two seconds. In the page load event, I have this code to set the meta tag:

C#
if (this.Master.Page.Header != null)
{
 HtmlHead hh = this.Master.Page.Header;
 HtmlMeta hm = new HtmlMeta();
 hm.Attributes.Add("http-equiv", "Refresh");
 hm.Attributes.Add("content", "2");
 hh.Controls.Add(hm);
}

Note: My sample web project has a master page, so setting the meta tag is a little different than if you didn't have a master page.

Conclusion

I found some interesting ways to auto refresh my web page. I hope you have learned something too. I know AJAX is better for partial page updates, but in this case, JavaScript worked fine and I didn't have to load AJAX on my web server.

License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)


Written By
Software Developer (Senior)
United States United States
I started my programmer career over 26 years ago doing COBOL and SAS on a MVS mainframe. It didn't take long for me to move into windows programming. I started my windows programming in Delphi (Pascal) with a Microsoft SQL server back end. I started working with vb.net when the beta 2 came out in 2001. After spending most of my programming life as a windows programmer I started to check out asp.net in 2004. I achieved my MCSD.net in April 2005. I have done a lot of MS SQL database stuff. I have a lot of experience with Window Service and Web services as well. I spent three years as a consultant programing in C#. I really enjoyed it and found the switch between vb.net and C# to be mostly syntax. In my current position I am programming in C# working on WPF and MSSql database stuff. Lately I have been using VS2019.

On a personal note I am a born again Christian, if anyone has any questions about what it means to have a right relationship with God or if you have questions about who Jesus Christ is, send me an e-mail. ben.kubicek[at]netzero[dot]com You need to replace the [at] with @ and [dot] with . for the email to work. My relationship with God gives purpose and meaning to my life.

Comments and Discussions

 
Question<Meta> tag refresh Pin
JaironLanda1-Feb-16 2:37
JaironLanda1-Feb-16 2:37 
AnswerRe: <Meta> tag refresh Pin
kubben1-Feb-16 3:02
kubben1-Feb-16 3:02 
QuestionError in code Pin
Er. Vikas Sangal14-Sep-15 3:43
Er. Vikas Sangal14-Sep-15 3:43 
GeneralMy vote of 2 Pin
Mohan Narayanan18-Mar-15 1:15
Mohan Narayanan18-Mar-15 1:15 
GeneralRe: My vote of 2 Pin
kubben18-Mar-15 1:42
kubben18-Mar-15 1:42 
Questioncan you make VB version Pin
ikuto tohoin25-Jan-11 12:48
ikuto tohoin25-Jan-11 12:48 
AnswerRe: can you make VB version Pin
kubben25-Jan-11 13:23
kubben25-Jan-11 13:23 
QuestionHow do I force a browser web-page refresh after re-creating the page again? Pin
mstod0017-Jul-09 12:25
mstod0017-Jul-09 12:25 
AnswerRe: How do I force a browser web-page refresh after re-creating the page again? Pin
kubben18-Jul-09 1:16
kubben18-Jul-09 1:16 
GeneralRe: How do I force a browser web-page refresh after re-creating the page again? Pin
mstod0018-Jul-09 5:07
mstod0018-Jul-09 5:07 
GeneralRe: How do I force a browser web-page refresh after re-creating the page again? Pin
mstod0018-Jul-09 5:11
mstod0018-Jul-09 5:11 
GeneralRe: How do I force a browser web-page refresh after re-creating the page again? Pin
kubben18-Jul-09 8:52
kubben18-Jul-09 8:52 
GeneralThanks Pin
Jon-Hawkins9-May-08 22:10
Jon-Hawkins9-May-08 22:10 
GeneralRe: Thanks Pin
kubben10-May-08 0:44
kubben10-May-08 0:44 
GeneralNot Working after a file dialogue Pin
CodeHook27-Jan-08 19:10
CodeHook27-Jan-08 19:10 
GeneralRe: Not Working after a file dialogue Pin
kubben28-Jan-08 1:42
kubben28-Jan-08 1:42 
GeneralRe: Not Working after a file dialogue Pin
CodeHook29-Jan-08 21:44
CodeHook29-Jan-08 21:44 
GeneralRe: Not Working after a file dialogue Pin
kubben30-Jan-08 2:06
kubben30-Jan-08 2:06 
GeneralRe: Not Working after a file dialogue Pin
CodeHook4-Feb-08 21:51
CodeHook4-Feb-08 21:51 
GeneralRe: Not Working after a file dialogue Pin
kubben5-Feb-08 0:59
kubben5-Feb-08 0:59 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.