Timer

The ASP.NET AJAX Timer control performs postbacks at defined intervals. If you use the Timer control with an UpdatePanel control, you can enable partial-page updates at a defined interval. You can also use the Timer control to post the whole page.

The Timer control requires specific settings in a web.config file in order to function correctly. If you try to work with this control, and your site doesn't contain the required web.config file, errors appear in the Design view of the page where the control would have appeared. In Design view, if you click the control that is in that state, Microsoft Expression Web will give you the option to create a new web.config file or update your existing web.config file.

Scenarios

You use the Timer control when you want to do the following:

  • Periodically update the contents of one or more UpdatePanel controls without refreshing the whole web page.

  • Run code on the server every time that a Timer control causes a postback.

  • Synchronously post the whole web page to the web server at defined intervals.

Background

The Timer control is a server control that embeds a JavaScript component into the web page. The JavaScript component initiates the postback from the browser when the interval that is defined in the Interval property has elapsed. You set the properties for the Timer control in code that runs on the server and those properties are passed to the JavaScript component.

An instance of the ScriptManager class must be included in the web page when you use the Timer control.

When a postback was initiated by the Timer control, the Timer control raises the Tick event on the server. You can create an event handler for the Tick event to perform actions when the page is posted to the server.

Set the Interval property to specify how often postbacks will occur, and set the Enabled property to turn the Timer on or off. The Interval property is defined in milliseconds and has a default value of 60,000 milliseconds, or 60 seconds.

Note

Setting the Interval property of a Timer control to a small value can generate significant traffic to the web server. Use the Timer control to refresh the content only as often as necessary.

You can include more than one Timer control on a web page if different UpdatePanel controls must be updated at different intervals. Alternatively, a single instance of the Timer control can be the trigger for more than one UpdatePanel control in a web page.

Using a Timer Control Inside an UpdatePanel Control

When the Timer control is included inside an UpdatePanel control, the Timer control automatically works as a trigger for the UpdatePanel control. You can override this behavior by setting the ChildrenAsTriggers property of the UpdatePanel control to false.

For Timer controls inside an UpdatePanel control, the JavaScript timing component is re-created only when each postback finishes. Therefore, the timed interval does not start until the page returns from the postback. For instance, if the Interval property is set to 60,000 milliseconds (60 seconds) but the postback takes 3 seconds to complete, the next postback will occur 63 seconds after the previous postback.

Using a Timer control outside an UpdatePanel control

When the Timer control is outside an UpdatePanel control, you must explicitly define the Timer control as a trigger for the UpdatePanel control to be updated.

If the Timer control is outside an UpdatePanel control, the JavaScript timing component continues to run as the postback is being processed. For example, if the Interval property is set to 60,000 milliseconds (60 seconds) and the postback takes 3 seconds to complete, the next postback will occur 60 seconds after the previous postback. The user will see the refreshed content in the UpdatePanel control for only 57 seconds.

You must set the Interval property to a value that enables one asynchronous postback to complete before the next postback is initiated. If a new postback is initiated while an earlier postback is being processed, the first postback is canceled.

For more information about the Timer control, see Timer Control Overview Cc295201.xtlink_newWindow(en-us,Expression.40).png in the MSDN Library.

For a description of all Timer control properties, see Timer Properties Cc295201.xtlink_newWindow(en-us,Expression.40).png in the MSDN library.

See also

Tasks

Tutorial: How to refresh an UpdatePanel control at a timed interval

Concepts

ASP.NET Ajax controls
Partial-page rendering overview
ScriptManager

Send feedback about this topic to Microsoft. © 2011 Microsoft Corporation. All rights reserved.