简体   繁体   English

asp.net计时器和response.redirect

[英]asp.net timer and 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
}

How can i redirect my page. 我如何重定向我的页面。 Is there any way it can be done by js or anything else? 有什么办法可以通过js或其他方式完成吗?

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. 发出页面后,发送重定向已为时已晚:响应已发送,因此您拥有的响应对象不再有效。

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. 或者在响应中设置一个Refresh标头,以告知客户端此时需要加载另一个页面。

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. 同样,从这样的页面内部实例化一个Timer可能不是一个好主意-除了它持有无效的响应对象外,即使访问者关闭了该页面,该计时器也将徘徊,这是一个相当昂贵的对象系统资源。

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; 另一方面,如果您只想在后台检查该条件,请从Application_OnStart事件在global.asax设置计时器,并让它更新volatile布尔变量; check this variable at the beginning of the page you want to conditionally redirect. 在您要有条件重定向的页面的开头检查此变量。

EDIT: 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 . 编辑:如果要检查显示该页面的每个浏览器中每个打开的窗口的条件,则必须使用脚本或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: 最简单的方法是让您的JavaScript间隔计时器定期尝试导航到特殊的“条件重定向器”页面,该页面仅由以下代码组成,该代码检查条件并重定向或不重定向页面:

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

Note that navigating to a URL that returns 204 "No Content" status causes the browser to leave the existing page alone. 请注意,导航到返回204“ No Content”状态的URL会导致浏览器将现有页面保留下来。 Also, keep in mind that this redirector page will be bombarded heavily, so keep your check lightweight. 另外,请记住,此重定向器页面将受到严重轰炸,因此请保持检查轻便。

 string myStringVariable = "Password Update!";
        ClientScript.RegisterStartupScript(this.GetType(), "myalert", "alert('" + myStringVariable + "');", true);

        Response.AddHeader("REFRESH", "1;URL=Login.aspx");
    <script language="javascript">
    function redirectFunction() {
        document.location.href = 'www.yourpage.com';
    }
    </script>
<body onload="javascript:setTimeout('redirectFunction();', 1000)"> 

Then on the load you can handle the logic if db.getvalue() = true then redirect to page. 然后,如果db.getvalue()= true,则可以在加载时处理逻辑,然后重定向到页面。
To avoid the whole page refreshing, do some googling on making ajax calls from javascript. 为避免整个页面刷新,请对通过JavaScript进行Ajax调用进行一些搜索。

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

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