简体   繁体   English

自动重定向页面

[英]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. “ 60”是页面重定向之前等待的时间(以秒为单位)。

Try this one line code: Here 5 means redirecting after 5 seconds, and make it 60 if you want to redirect after 1 minute. 尝试以下一行代码:这里的5表示5秒后重定向,如果要在1分钟后重定向,则将其设置为60。

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. 您还可以将此代码放入页面的Load事件中,以便在加载当前页面后将其重定向到另一页面。

You cannot use C# code to redirect after a certain time from the server side, since C# is executed on server side. 由于C#是在服务器端执行的,因此您不能在一段时间后使用C#代码从服务器端重定向。 You can do this by having the meta tag in your HTML: 您可以通过在HTML中添加meta标签来做到这一点:

<meta http-equiv="refresh" content="300; url=newlocation">

You can write code in C# to create this tag, Here is an example: 您可以用C#编写代码来创建此标签,这是一个示例:

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. 注意:SpinWait参数是循环计数,而不是上面建议的秒数。

Taken from MSDN page http://msdn.microsoft.com/en-us/library/system.threading.thread.spinwait.aspx 取自MSDN页面http://msdn.microsoft.com/en-us/library/system.threading.thread.spinwait.aspx

The SpinWait method is useful for implementing locks. SpinWait方法对于实现锁很有用。 Classes in the .NET Framework, such as Monitor and ReaderWriterLock, use this method internally. .NET Framework中的类(例如Monitor和ReaderWriterLock)在内部使用此方法。 SpinWait essentially puts the processor into a very tight loop, with the loop count specified by the iterations parameter. SpinWait本质上将处理器置于一个非常紧密的循环中,循环计数由迭代参数指定。 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. 您需要使用javascript设置计时器,然后重定向。

See this on how to redirect: How to redirect to another webpage in JavaScript/jQuery? 看到有关如何重定向的信息: 如何在JavaScript / jQuery中重定向到另一个网页?

See this for timers: 计时器请参见以下内容:
Loop timer in javascript JavaScript中的循环计时器

http://www.w3schools.com/js/js_timing.asp http://www.w3schools.com/js/js_timing.asp

http://www.elated.com/articles/javascript-timers-with-settimeout-and-setinterval/ http://www.elated.com/articles/javascript-timers-with-settimeout-and-setinterval/

I love doing my stuff in JavaScript :-) I love JS. 我喜欢在JavaScript中做我的工作:-)我喜欢JS。 Here is my JS solution . 这是我的JS解决方案。

<script type="text/javascript"><!--
setTimeout('Redirect()',4000);
function Redirect()
{
  location.href = 'your-redirect-to-link';
}

// --></script>

The page will be redirected after 4 minutes. 该页面将在4分钟后重定向。 You have to insert that into the head obviously. 您显然必须将其插入头部。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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