简体   繁体   中英

redirect a page automatically

如何在1分钟后使用c#代码自动将ASP.NET页面重定向到另一个页面。

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.

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.

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);  

you can do so using:

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

Edit:

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

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.

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);

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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM