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" />
...
</head>
Using ASP.NET (WebForms) Code to add the Meta Header
You can also do this in code in an ASP.NET WebForms page by adding an HtmlMeta() control to the Page.Header.Controls collection like this:
// Classic ASP.NET - WebForms
// *** 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);
Sending HTTP Headers directly
But I never put 2 and 2 together to realize that the meta tag is actually mapping an actual HTTP Header. Meta tags are essentially page level overrides to actual Http headers, so meta-refresh refers to the Refresh Http header.
So, from a server generated application, it's actually much easier to add a refresh by adding the following to the Response headers like this:
Response.AppendHeader("Refresh", "4");
This refreshes the current page after 4 seconds by reloading itself.
Alternately to go to a different page:
Response.AppendHeader("Refresh", "4; url=/profile");
This is of course much shorter and 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.
Where can you do this?
Adding Http headers via Response.AppendHeader() can be done inside of the ASP.NET request processing. Since ASP.NET always caches responses you can delay adding the header until the very end of a request which allows you to conditionally set it based on Page logic.
Response.AppendHeader() is also available in HttpHandlers and HttpModuless where the same up until the end rules apply.
Note: ASP.NET Core and other Web platforms do not cache headers and require that headers are written before any other content is sent to the
Response.
Ah, the little things that one can easily overlook that sometimes make things easier...
Other Posts you might also like
- Map Physical Paths with an HttpContext.MapPath() Extension Method in ASP.NET
- Adding minimal OWIN Identity Authentication to an Existing ASP.NET MVC Application
- Getting the Client IP Address in ASP.NET Core
- Resolving Paths To Server Relative Paths in .NET Code
- Getting the ASP.NET Core Server Hosting Urls at Startup and in Requests
The Voices of Reason
# re: No more Meta Refresh Tags
# re: No more Meta Refresh Tags
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
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
It's not cross browser capatible...but it also doesn't really break anything in FF.
thanks
# re: No more Meta Refresh Tags
# re: No more Meta Refresh Tags
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
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
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
# re: No more Meta Refresh Tags
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
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.
# re: No more Meta Refresh Tags
# re: No more Meta Refresh Tags
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
what a clean way of doing things :D
# re: No more Meta Refresh Tags
# re: No more Meta Refresh Tags
# re: No more Meta Refresh Tags
# re: No more Meta Refresh Tags
Response.ClearHeaders();
before the
Response.AppendHeader("Refresh", "4; url=profile.aspx");
/Yero
# re: No more Meta Refresh Tags
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
# re: No more Meta Refresh Tags
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
# re: No more Meta Refresh Tags
# re: No more Meta Refresh Tags
# re: No more Meta Refresh Tags
# re: No more Meta Refresh Tags
@Rick 17 yrs later this page was still helpfull.
# re: No more Meta Refresh Tags