Redirect to another page after a few seconds in ASP.NET
A "you will be redirected in 5 seconds" page is common after a form submission, a logout or when content has moved. There are three ways to do it in an ASP.NET page; the JavaScript version is the friendliest because it can show a countdown and still offers a link.
1. The Refresh header from code-behind
protected void Page_Load(object sender, EventArgs e)
{
// "5; url=..." : wait 5 seconds, then load the URL
Response.AppendHeader("Refresh", "5; url=" + ResolveUrl("~/Default.aspx"));
}
Browsers honour the Refresh header even though it was never part of the HTTP standard; HTML defines the equivalent meta element. Use relative or same-site URLs only. Never put a URL taken from the query string into this header without checking it, or the page becomes an open redirect that attackers can use in phishing links.
2. A meta refresh tag
<head runat="server">
<meta http-equiv="refresh" content="5; url=Default.aspx" />
<title>Thank you</title>
</head>
Or add it from code so the target can vary:
var meta = new HtmlMeta { HttpEquiv = "refresh", Content = "5; url=" + ResolveUrl("~/Orders.aspx") };
Page.Header.Controls.Add(meta);
3. A JavaScript countdown
<p>Your message has been sent. You will be taken to the home page in
<strong id="seconds">5</strong> seconds.</p>
<p><a id="next" href="<%= ResolveUrl("~/Default.aspx") %>">Go now</a></p>
<script>
(function () {
var left = 5;
var counter = document.getElementById("seconds");
var target = document.getElementById("next").href;
var timer = setInterval(function () {
left -= 1;
counter.textContent = left;
if (left <= 0) {
clearInterval(timer);
window.location.assign(target);
}
}, 1000);
})();
</script>
The link works without JavaScript and lets impatient users skip the wait. location.assign keeps the current page in history; use location.replace if the back button should not return to the waiting page.
Showing it after a postback
For a "saved, returning to the list" message, register the script only after a successful save:
protected void btnSave_Click(object sender, EventArgs e)
{
SaveOrder();
pnlForm.Visible = false;
pnlDone.Visible = true; // contains the countdown markup above
}
When not to wait
- Moved pages: send a permanent redirect immediately with
Response.RedirectPermanent(url)(301). Search engines transfer the old URL's signals to the new one; a timed refresh is weaker and confuses users. - After a POST: the Post/Redirect/Get pattern (
Response.Redirectright after saving) prevents duplicate submissions when the user refreshes. - Accessibility: WCAG asks that time limits can be turned off or extended, and recommends against timed redirects (technique F41). A short delay with a visible link is acceptable for confirmation pages; for anything the user needs to read, do not redirect automatically.
Refreshing the same page periodically is a different need, covered in Auto-refresh a page every 10 seconds. Redirecting on a button click is in Redirect on button click.