Reputation: 71
how to automatically redirect an ASP.NET page to another after 1 minute using c# code.
Upvotes: 7
Views: 26141
Reputation: 4177
You can use something like this:
<meta http-equiv="Refresh" content="60; url=http://your.new/url/here" />
The "60" is the time in seconds to wait before page redirect.
Upvotes: 20
Reputation: 449
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);
Upvotes: 1
Reputation: 3050
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.
Upvotes: 10
Reputation: 942
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.
Upvotes: 0
Reputation: 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.
Upvotes: 2
Reputation: 49
you can do so using:
System.Threading.Thread.Wait(60);
Response.Redirect("Somepage.aspx");
Edit:
System.Threading.Thread.SpinWait(60);
Response.Redirect("Somepage.aspx");
Upvotes: 2
Reputation: 19353
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);
Upvotes: 6
Reputation: 19465
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/
Upvotes: 0