Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Mike Lewis on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Response.Redirect() works from Page_Load() but not timer1_elapsed()

Status
Not open for further replies.

MuadDubby

Programmer
Sep 23, 1999
236
CA
Hello

I'm trying to automatically redirect the user to another site after a timer expires, and the redirection just doesn't happen. I've found posts talking about stopping the timer and disabling SmartNavigation, but nothing helps.

If I redirect from the Form_Load() event, though, no problem. The browser goes to the new site. Any ideas?

Here's my code:
Code:
private void Page_Load(object sender, System.EventArgs e)
{
	// this redirection works
	Response.Redirect("[URL unfurl="true"]http://www.microsoft.com",false);[/URL]
}

private void timer1_Elapsed(object sender, System.Timers.ElapsedEventArgs e)
{
	timer1.Stop();
	Page.SmartNavigation = false;
	// this redirection does not work; no exceptions are thrown
	Response.Redirect("[URL unfurl="true"]http://www.microsoft.com",[/URL] false);
}
 
The redirection timer has to be on the client-side, but using javascript.

When Page_Load exits, ASP.NET (after other parsing tasks) returns the response back to the browser, and doesn't wait for the timer (since the timer runs on another thread). For more information on this matter, post this query on the ASP.NET forum.

my 2 cents [wink]
 
The web pages are stateless. After processing the code the HTML is sent to the browser and then there is nothing...it's all gone.
The Timer is a .NET class which is useful when your code is running in .NET. Your ASP.NET code runs inside the .NET but for a brief period which produces state-less webpages. Once it generates and finishes code running, it no longer has any execution context in which it would utilize an instance of this Timer class.
You need to use client-side JavaScript for this. Use the javascript setTimeout() method to change the browser URL to the page you want.
Code:
url="[URL unfurl="true"]http://www.tek-tips.com/"[/URL]
setTimeout("location.href=url",10000)

Sharing the best from my side...

--Prashant--
 
Thank you Prashant.

I did a few searches on the setTimeout function, and found some recommendations to do the redirection through META tags whenever possible, just in case javascript is not supported.

The tags are like this:
Code:
<meta http-equiv="refresh" content="10; URL=http://www.cnn.com">
<meta name="keywords" content="automatic redirection">

and they work like a charm. What would your preference be? Javascript or META?

 
Always META...because Javascript on client can be disabled. I forgot to make that point in my last submission.
No matters...a good solution.
Cheers...[2thumbsup]

Sharing the best from my side...

--Prashant--
 
the timer is running on a different thread than the UI. this is why you are experiencing the failure.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top