7

how to automatically redirect an ASP.NET page to another after 1 minute using c# code.

Karthik Malla
  • 71
  • 1
  • 1
  • 2

8 Answers8

20

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.

Sracanis
  • 490
  • 5
  • 25
Laur Ivan
  • 4,117
  • 3
  • 38
  • 62
10

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.

Raaghav
  • 3,000
  • 1
  • 23
  • 21
6

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);  
Abdel Raoof Olakara
  • 19,223
  • 11
  • 88
  • 133
2

you can do so using:

System.Threading.Thread.Wait(60); 
Response.Redirect("Somepage.aspx");

Edit:

System.Threading.Thread.SpinWait(60);
Response.Redirect("Somepage.aspx");
Pooya
  • 49
  • 3
  • 8
2

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.

Bo Persson
  • 90,663
  • 31
  • 146
  • 203
Mych
  • 21
  • 1
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);
Mike
  • 419
  • 1
  • 6
  • 22
0

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/

Community
  • 1
  • 1
gideon
  • 19,329
  • 11
  • 72
  • 113
0

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.

Nerudo
  • 902
  • 1
  • 7
  • 11