Contact   •   Products   •   Search

Rick Strahl's Web Log

Wind, waves, code and everything in between...
ASP.NET • C# • HTML5 • JavaScript • AngularJs

No more Meta Refresh Tags


From time to time I have pages that need to auto-refresh or more commonly a page that automatically needs to go to another page after showing a short informational message. In the past I’ve always done this with Meta-Refresh tags in the page which looks like this:

 

<html xmlns="http://www.w3.org/1999/xhtml">

<head><title>

      Welcome Back

title>

<meta http-equiv="Refresh" content="2; URL=/wwstore/Profile.aspx" />

head>

 

You can add this into an ASP.NET page with code like this:

 

// *** Create META tag and add to header controls

HtmlMeta RedirectMetaTag = new HtmlMeta();

RedirectMetaTag.HttpEquiv = "Refresh";

 

RedirectMetaTag.Content = string.Format("{0}; URL={1}", this.Context.Items["ErrorMessage_Timeout"], NewUrl);

this.Header.Controls.Add(RedirectMetaTag);

 

But I never put 2 and 2 together to realize that the meta tag is actually mapping an HTTP header. A much easier way to do this is to simply add a header:
 

Response.AppendHeader("Refresh", "4");

 

Or refresh and go off to another page:

 

Response.AppendHeader("Refresh", "4; url=profile.aspx");

 

Duh… much cleaner. One advantage of the Meta tag approach is that if somebody bookmarks the page the Meta-Refresh will still fire even on a page that is browser cached, while the HTTP header most likely wouldn’t.

 

Ah, the little obvious things one overlooks sometimes…

Make Donation


Feedback for this Post

 
# re: No more Meta Refresh Tags
by Peter Bromberg August 04, 2006 @ 5:50pm
Well! That was, uh -- Refreshing!
# re: No more Meta Refresh Tags
by hagay August 06, 2006 @ 8:17am
does it work with firefox ? seems here it doesnt ...
# re: No more Meta Refresh Tags
by Rick Strahl August 06, 2006 @ 1:54pm
Yes it does here... I also wasn't sure so I tried all the browsers I had installed which is IE 7, FF 1.5, Opera 9.0.

You'll want to make sure if you're redirecting to another page the page URL is correct. So you may have to use ResolveUrl() to get the URL reference right.

And if you're not going to another page - make sure you actually notice when the page changes <g>... on a couple of my pages the refresh is so quick the page barely blinks <s>...
# re: No more Meta Refresh Tags
by Oskar Austegard August 07, 2006 @ 7:23am
This also works nicely in ASP.NET 1.1.

Couple this with the Fajax trick (http://secretgeek.net/fajax.asp) and you have real fake ajax on the cheap.
# re: No more Meta Refresh Tags
by AC August 17, 2006 @ 12:36pm
hey Rick, could you give an opinion about that Fajax trick?...do you see any value in using that..or is that just going to come across as a cheap gimmic to the enduser...like having an animated cartoon character doing the robot on your web page.

It's not cross browser capatible...but it also doesn't really break anything in FF.

thanks
# re: No more Meta Refresh Tags
by Rick Strahl August 17, 2006 @ 12:51pm
I don't see anything wrong with using this trick as long as you realize that you are in fact not doing anything AJAX. If you can make the user experience better by all means do it <g>...
# DotNetSlackers: No more Meta Refresh Tags
by DotNetSlackers Latest ASP.NET News October 12, 2006 @ 12:59am
# re: No more Meta Refresh Tags
by David November 21, 2006 @ 4:49pm
Hello:

I tried your trick in asp.net 2.0 with MS Ajax 2.0. Inside an Update Panel I insert a GridView.

The first time that the page is render, the "refresh" works fine, but if you press something inside the gridview that cause a postback, the refresh tag is not triggered.

What could be the problem?

Thanks in advance.
# re: No more Meta Refresh Tags
by Rick Strahl November 21, 2006 @ 4:56pm
HTTP Headers can't be updated in an AJAX callback. The page is already there, so you can't change the header for that page.

If you want the page to refresh you have to use client side code with a timer to change location and hook that through the script manager:

Something like this:

this.ScriptManager.RegisterStartupScript(this.Type(), "Refresh", "window.setTimeout('window.location=''thisPage.aspx''',5000)",true);
# re: No more Meta Refresh Tags
by David November 23, 2006 @ 1:03pm
Thanks for your answer Rick, but my situation is a little bit more complicated, because I included your code to inject the javascript "refresh", but my purpose is to reset the refresh timeout every time that a client raises a postback.

For example:

You have your session timeout defined in 10 minutes. And your script will refresh and redirect your page to another when a user browse your asp page without do nothing during 10 minutes. But suppose that the user press something that initiates a postback after 9 minutes. What I need is to reset the interval again, otherwise in 1 minute the refresh javascript will be raised and I dont want that.
# re: No more Meta Refresh Tags
by Rick Strahl November 23, 2006 @ 4:08pm
If you're doing a postback you can just reset the Refresh header on the postback. If you're doing a Callback then you can use the code I showed above. That should handle either situation.
# re: No more Meta Refresh Tags
by kenneth January 12, 2007 @ 10:41pm
how would you handle a situation where the page with the desired timeout is fully encapsulated in an ajax .net update panel - and therefore the controls on the page only cause (partial) postbacks - which dont actually reset the timeout..?

neither the meta tag nor the window.setTimeout method seem to work. the user can continuously be manipulating the controls - and apparently because there's no full-postbacks - the timeouts keep kicking in.

any ideas would be appreciated-

kenneth
# re: No more Meta Refresh Tags
by Rick Strahl January 13, 2007 @ 2:21pm
If you're using AJAX on the page you can use setTimeout() and manipulate the location object in the callback handler.

The header of the page obviously won't be changed if you're doing partial postbacks since hte page itself including all of its headers don't change. I seem to remember some talk that there's some logic in UpdatePanel that does deal with Response.Redirect() calls, but I haven't tried that because frankly that seems silly in that it's very un-Ajax like. It's best to deal with these kind of tasks via client script in Ajax code.
# どっとねっとふぁんBlog : MetaタグでRefreshの代わりにHttpヘッダーでRefresh
by どっとねっとふぁんBlog July 04, 2007 @ 5:32pm
# Auto Refresh Webpage - ASP.NET Forums
by ASP.NET Forums July 05, 2007 @ 2:22pm
# re: No more Meta Refresh Tags
by Muthu kumaran August 22, 2007 @ 11:16pm
Cool code man.. Thanks for your code..
# re: No more Meta Refresh Tags
by Vivek October 19, 2007 @ 5:47am
hi,
This is quite intresting post.
But it would have been a great help if you have explained a fewer terms for some novice programmers.
like:
RedirectMetaTag.Content = string.Format("{0}; URL={1}", this.Context.Items["ErrorMessage_Timeout"], NewUrl);

What does "this.Context.Items["ErrorMessage_Timeout"]" mean?
& what is "NewUrl"?
# re: No more Meta Refresh Tags
by winnie March 22, 2008 @ 12:58pm
wow! exactly what i need
what a clean way of doing things :D
# re: No more Meta Refresh Tags
by Eric May 22, 2009 @ 8:04am
This eliminated a LOT of code from my page. THANKS!
# re: No more Meta Refresh Tags
by Mike Kingscott December 16, 2009 @ 5:17am
Hmmm. I wonder how the Response.AppendHeader would be liked by WorldPay? We've recently been advised to use a META refresh to redirect people who land on our payment result page (hosted by WorldPay), as they no longer allow scripted redirects (in addition to response.redirects, or server.transfers) as part of their recent XSS changes. If, as you say, appending the redirect to the Response does the same thing, it's a lot less code, but I'll guess I'll have to suck it and see.
# re: No more Meta Refresh Tags
by Rick Strahl December 16, 2009 @ 1:26pm
@Mike - it's the same thing. The idea of a Refresh is that the browser navigates. The target site sees the same thing.
# re: No more Meta Refresh Tags
by Yeroon January 27, 2010 @ 1:44am
Great example. However I couldnt get it to work without adding:

Response.ClearHeaders();

before the

Response.AppendHeader("Refresh", "4; url=profile.aspx");

/Yero
# re: No more Meta Refresh Tags
by Manish October 19, 2010 @ 10:56pm
Still not working for me.....
Can u please tell me how to clear values of meta tag!!!!!!!!!!!
I t is really important for me !!!!!!!!!!
# re: No more Meta Refresh Tags
by D April 16, 2012 @ 2:19pm
Awesome! Works well.
# re: No more Meta Refresh Tags
by William Walseth May 03, 2012 @ 10:31am
The Refresh in IE 9 appears to work in "seconds", not minutes.
Here's my simple auto log-out code
Session.Timeout = 10; // time-out in 10 minutes...
Response.AppendHeader("Refresh", "600; url=logon.aspx"); // re-direct after time-out
# re: No more Meta Refresh Tags
by Neelima July 08, 2012 @ 10:19pm
Superb Code man!!!! Thanks allot!! Exactly this was what i need...
# re: No more Meta Refresh Tags
by Esteban August 02, 2012 @ 10:05am
I tried the example but it only works the first time. I need time to refresh the session because I have a Silverlight application hosted on the page and never generates a postback or refresh every 20 minutes exactly But it refreshes the page and lose all the work they had done.
# re: No more Meta Refresh Tags
by CleveSteve March 28, 2013 @ 4:18am
The one line of code that you have in your punchline... Is that to be placed in the Page_Load event if I want the page to refresh at regular intervals? Is there code I can add to reset or inhibit the refresh in page control events? Like RowEditing or something like that? Thanks!
# re: No more Meta Refresh Tags
by Rick Strahl March 28, 2013 @ 4:56am
@CleveSteve - you can put this anywhere really. Headers aren't sent until the page rendering completes since it's all buffered.
 


West Wind  © Rick Strahl, West Wind Technologies, 2005 - 2014