Reputation: 1786
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?
Upvotes: 1
Views: 7939
Reputation: 1
string myStringVariable = "Password Update!";
ClientScript.RegisterStartupScript(this.GetType(), "myalert", "alert('" + myStringVariable + "');", true);
Response.AddHeader("REFRESH", "1;URL=Login.aspx");
Upvotes: 0
Reputation: 36524
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.
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.
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.
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
. 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;
%>
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.
Upvotes: 5
Reputation: 9193
<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.
To avoid the whole page refreshing, do some googling on making ajax calls from javascript.
Upvotes: 0