how to automatically redirect an ASP.NET page to another after 1 minute using c# code.
-
Does it must need be coded in `C#`? Or can you use a html meta refresh tag... – The Scrum Meister Mar 06 '11 at 12:58
8 Answers
Try this one line code: Here 5 means redirecting after 5 seconds, and make it 60 if you want to redirect after 1 minute.
protected void btnRedirect_Click(object sender, EventArgs e)
{
Response.AddHeader("REFRESH", "5;URL=YourNextPage.aspx");
}
This code you can also put in Load
event of the page so that it'll redirect to another page after loading current page.

- 3,000
- 1
- 23
- 21
You cannot use C# code to redirect after a certain time from the server side, since C# is executed on server side. You can do this by having the meta tag in your HTML:
<meta http-equiv="refresh" content="300; url=newlocation">
You can write code in C# to create this tag, Here is an example:
HtmlMeta meta = new HtmlMeta();
HtmlHead head = (HtmlHead)Page.Header;
meta.HttpEquiv= "refresh";
meta.Content = "300; url=newlocation";
head.Controls.Add(meta);

- 19,223
- 11
- 88
- 133
you can do so using:
System.Threading.Thread.Wait(60);
Response.Redirect("Somepage.aspx");
Edit:
System.Threading.Thread.SpinWait(60);
Response.Redirect("Somepage.aspx");

- 49
- 3
- 8
-
Thanks Pooya the above code seems correct but not working. Have you tested it? – Karthik Malla Mar 06 '11 at 13:32
-
oops! I missed something, you should use System.Threading.Thread.SpinWait(60) instead of that line. Good luck. – Pooya Mar 06 '11 at 17:21
-
Note: The SpinWait parameter is a cycle count and not seconds as the above suggests.
Taken from MSDN page http://msdn.microsoft.com/en-us/library/system.threading.thread.spinwait.aspx
The SpinWait method is useful for implementing locks. Classes in the .NET Framework, such as Monitor and ReaderWriterLock, use this method internally. SpinWait essentially puts the processor into a very tight loop, with the loop count specified by the iterations parameter. The duration of the wait therefore depends on the speed of the processor.

- 90,663
- 31
- 146
- 203

- 21
- 1
There are many ways to do this but I love to use this code because it works well when used in many different circumstances.
HtmlMeta oScript = new HtmlMeta();
oScript.Attributes.Add("http-equiv", "REFRESH");
oScript.Attributes.Add("content", "60; url='http://www.myurl.com/'");
Page.Header.Controls.Add(oScript);

- 419
- 1
- 6
- 22
Doing this on the client would be better than doing it on the server.
You'll need to use javascript to setup a timer and then redirect.
See this on how to redirect: How to redirect to another webpage in JavaScript/jQuery?
See this for timers:
Loop timer in javascript
http://www.w3schools.com/js/js_timing.asp
http://www.elated.com/articles/javascript-timers-with-settimeout-and-setinterval/
I love doing my stuff in JavaScript :-) I love JS. Here is my JS solution .
<script type="text/javascript"><!--
setTimeout('Redirect()',4000);
function Redirect()
{
location.href = 'your-redirect-to-link';
}
// --></script>
The page will be redirected after 4 minutes. You have to insert that into the head obviously.

- 902
- 1
- 7
- 11