asp.net定时器的Response.Redirect [英] asp.net timer and response.redirect

查看:225
本文介绍了asp.net定时器的Response.Redirect的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一些检查条件的计时器和条件成为真正有重定向的页​​面

i have a Timer that checks some condition and as the condition become true it has to redirect the page

     protected void Page_Load(object sender, EventArgs e)
    {
        if (!Page.IsPostBack)
        {
            /* Thread th = new Thread(new ThreadStart(OtherThread));
             th.Start();*/
            System.Timers.Timer t = new System.Timers.Timer(9000);
            t.Enabled = true;
            t.Elapsed += new System.Timers.ElapsedEventHandler(t_Elapsed);
            Session["total"] = 0;
        }
    }  
void t_Elapsed(object sender, System.Timers.ElapsedEventArgs e)
    {
if(db.getvalue()==true)
   response.redirect("abc.aspx"); //notworking
  //server.transfer("abc.aspx"); //not working
}

我怎样才能重定向我的网页。有什么办法可以通过JS或其他任何东西做什么?

How can i redirect my page. Is there any way it can be done by js or anything else?

推荐答案

一旦你发出的页面时,它已经太晚了发送重定向:响应已发送,让你有响应对象不再有效。

Once you have emitted a page, it's too late to send a redirect: the response has already been sent, so the response object you have is no longer valid.

相反,你需要处理它无论是在客户端脚本code的东西,如

Instead, you'll need to deal with it either in client-side script code with something like

window.setTimeout(function() { window.location.href = "blah.aspx"; }, 9000);

或响应告诉客户端,这将需要在那个时候加载不同的页面设置刷新头。

or by setting a Refresh header in the response to tell the client side that it will need to load a different page at that time.

此外,实例化一个定时从内这样的页面可能是一个坏主意 - 除了事实,即它是抱着一个无效的响应对象,计时器会流连即使访问者关闭网页,它在系统资源方面有相当昂贵的对象。

Also, instantiating a Timer from inside a page like that is probably a bad idea -- besides the fact that it's holding an invalid response object, the timer will hang around even if a visitor closes the page, and it's a fairly expensive object in terms of system resources.

在另一方面,如果你只是想检查的背景条件下,从你的 Application_OnStart的设立在的Global.asax 计时器事件,并将它更新挥发性布尔变量;检查这个变量在要有条件地重定向页面的开始。

On the other hand, if you just want to check that condition in the background, set up the timer in global.asax from your Application_OnStart event and have it update a volatile Boolean variable; check this variable at the beginning of the page you want to conditionally redirect.

编辑:如果您想进行检查的条件在每个浏览器每一个打开的窗口显示的页面,你将不得不使用任何脚本或刷新。也许,最简单有你的JavaScript计时器定期尝试导航到一个特殊的有条件的重定向器页面,包括以下code,检查的条件,要么重定向或独自离开页面的ONLY:

If you want the condition to be checked for every open window in every browser showing the page, you'll have to use either scripting or a Refresh. It might be simplest to have your JavaScript interval timer periodically try to navigate to a special "conditional redirector" page that consist ONLY of the following code that checks the condition and either redirects or leaves the page alone:

<%
    if (db.getvalue())  // saying "== true" is redundant
        response.Redirect("abc.aspx");
    else
        response.StatusCode = HttpStatusCode.NoContent;
%>

注意,导航到返回204的URL无内容状态将导致浏览器抛开现有页面。另外,请记住,此页面重定向器将严重炮轰,所以保持你的支票轻便。

Note that navigating to a URL that returns 204 "No Content" status causes the browser to leave the existing page alone. Also, keep in mind that this redirector page will be bombarded heavily, so keep your check lightweight.

这篇关于asp.net定时器的Response.Redirect的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆