Timer in Web Form

egdotnet

Regular
Joined
May 16, 2004
Messages
50
I'm trying to use a timer to redirect to a different page when the time elapses, but it's not working. Anyone have any idea why?

Here's the code:
Code:
    Private Sub Page_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
        Timer1.Start()
    End Sub

    Private Sub Timer1_Elapsed(ByVal sender As System.Object, ByVal e As System.Timers.ElapsedEventArgs) Handles Timer1.Elapsed
        Response.Redirect("http://www.google.com")
    End Sub

The timer's properties are:
autoreset = true
enabled = true
interval = 5000

Thanks!
 
I'm not quite sure how you got a timer to even declare in a webform - perhaps you included a reference to Windows.Forms.

Anyways, you can't use a windows forms timer control in a webform - or I should say that you can't use one to achieve a delayed redirect. Your webform's code executes on the webserver...and it ultimately builds a webpage and sends it to a web browser (client) then disconnects - there is no direct link or state maintained once the ASP.Net webform is done sending the page. That timer may be running and that event may be firing (somehow it just doesn't seem like it should) but it's not going to have a link to any page on a client so no client page is going to redirect as a result of that timer.

There are a couple of ways you could do a "redirect" for your page on the client-side. One would be to use a little bit of javascript:

Code:
<script language="javascript">
window.setTimeout("window.location.href='URL_to_direct_to.aspx", 5000);
</script>

That's just a generic javascript block - it'll run after the page loads. It changes the browsers URL, effectively "redirecting", and it use an interval in milliseconds just like a timer - so the 5000 above represents a 5 second delay before "redirection" occurs. You can either put that somewhere in your .aspx file (either between the HEAD tags or at the bottom just before the closing BODY tag - although it could go almost anywhere) or have your codebehind build it up and either response.write it or register it with one of the script registering methods of the Page object.

Another way to do it is with an html META tag (that you place between the HEAD /HEAD tags in your (aspx) page). It looks like this:

Code:
<META HTTP-EQUIV="Refresh" CONTENT="3;URL=URL_TO_DIRECT_TO.aspx">

This one is telling the browser to "redirect" to another URL after 3 seconds (it uses an interval in seconds instead of milliseconds. You would probably want to setup this META tag in your aspx page, although if you know how to manipulate HtmlGeneric controls (which don't have to be between a runat=server form tag) then you can technically control this from codebehind too).

Good luck,
Paul
 
egdotnet said:
but there is a timer component in web forms. what's it for?

According to MSDN there 3 types of timers - one that appears in the Windows "tab" when you work with windows forms, a server-based timer that shows up in the componenet "tab" of the toolbox, and another. I think you're seeing the "server-based" version when working with webforms and I don't think it's intended use matches what you want it to do. Check out the overview and remarks for the timer control and then follow the link for specifics on the derver-side version for more.

Paul
 
Back
Top