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.
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.
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.
PreRender of the
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)
var reloadTimer = null;
var sURL = unescape(window.location.pathname);
if (arguments.length == 1)
if (reloadTimer) clearTimeout(reloadTimer);
reloadTimer = setTimeout("setReloadTime()",
Math.ceil(parseFloat(secs) * 1000));
reloadTimer = null;
window.location.replace( sURL );
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:
<meta http-equiv="Refresh" content="n;url"/>
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:
if (this.Master.Page.Header != null)
HtmlHead hh = this.Master.Page.Header;
HtmlMeta hm = new HtmlMeta();
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.