Export (0) Print
Expand All

Timer Class

Performs asynchronous or synchronous Web page postbacks at a defined interval.

Namespace:  System.Web.UI
Assembly:  System.Web.Extensions (in System.Web.Extensions.dll)

[AspNetHostingPermissionAttribute(SecurityAction.InheritanceDemand, Level = AspNetHostingPermissionLevel.Minimal)]
[AspNetHostingPermissionAttribute(SecurityAction.LinkDemand, Level = AspNetHostingPermissionLevel.Minimal)]
public class Timer : Control, IPostBackEventHandler, 
	IScriptControl
<asp:Timer />

The Timer control enables you to perform postbacks at a specified interval. When you use the Timer control as a trigger for an UpdatePanel control, the UpdatePanel control is updated by using an asynchronous, partial-page update. You must include a ScriptManager object in a Web page to use the Timer control.

You use the Timer control to update an UpdatePanel control by including the timer inside the UpdatePanel control. Alternatively, you can place the timer outside the UpdatePanel control and set the timer as a trigger.

You can also initiate full postback of a complete Web page by including the Timer control in the Web page and not setting it as a trigger for an UpdatePanel control.

You can run server code when the timer interval has elapsed by creating an event handler for the Tick event. In the event handler, you can include code that dynamically adjusts the behavior of the Timer control.

Set the Interval property to specify how often postbacks will occur. Set the Enabled property to turn the Timer on or off.

NoteNote:

The accuracy of when the Timer control posts back to the Web server depends on the accuracy of the ECMAScript (JavaScript) window.setTimeout function that is running in the browser.

The Interval property is defined in milliseconds. You must set the Interval property to a value that lets at least one asynchronous postback complete before the next postback is initiated. When the Timer control is outside an UpdatePanel control, the timer continues to run while the content of the UpdatePanel control is refreshed. If a new postback is initiated while an earlier postback is being processed, the first postback will be canceled. The default value is 60,000 milliseconds (60 seconds).

NoteNote:

Adding a Timer control with a small value in the Interval property can generate significant traffic to and from the Web server. Use the Timer control to refresh the content only as often as necessary.

The following example shows an UpdatePanel control that displays a randomly generated stock price and the time that the stock price was generated. The Timer control updates the content in the UpdatePanel control every 10 seconds.

NoteNote:

In this example, the timer interval is set to 10 seconds so that when you run the example, you do not have to wait a long time to see the results. Because each timer interval causes a postback that causes network traffic, in a production application you should set the interval to the longest time that is still practical for your application.

<%@ Page Language="C#" AutoEventWireup="true" %>

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.1//EN" "http://www.w3.org/TR/xhtml11/DTD/xhtml11.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
    <title>Timer Example Page</title>
    <script runat="server">
        protected void Page_Load(object sender, EventArgs e)
        {
            OriginalTime.Text = DateTime.Now.ToLongTimeString();
        }

        protected void Timer1_Tick(object sender, EventArgs e)
        {
            StockPrice.Text = GetStockPrice();
            TimeOfPrice.Text = DateTime.Now.ToLongTimeString();
        }

        private string GetStockPrice()
        {
            double randomStockPrice = 50 + new Random().NextDouble();
            return randomStockPrice.ToString("C");
        }
    </script>
</head>
<body>
    <form id="form1" runat="server">
        <asp:ScriptManager ID="ScriptManager1" runat="server" />
        <asp:Timer ID="Timer1" OnTick="Timer1_Tick" runat="server" Interval="10000" />

        <asp:UpdatePanel ID="StockPricePanel" runat="server" UpdateMode="Conditional">
        <Triggers>
            <asp:AsyncPostBackTrigger ControlID="Timer1" />
        </Triggers>
        <ContentTemplate>
            Stock price is <asp:Label id="StockPrice" runat="server"></asp:Label><BR />
            as of <asp:Label id="TimeOfPrice" runat="server"></asp:Label>  
        </ContentTemplate>
        </asp:UpdatePanel>
        <div>
        Page originally created at <asp:Label ID="OriginalTime" runat="server"></asp:Label>
        </div>
    </form>
</body>
</html>

Any public static (Shared in Visual Basic) members of this type are thread safe. Any instance members are not guaranteed to be thread safe.

Windows 7, Windows Vista, Windows XP SP2, Windows Server 2008 R2, Windows Server 2008, Windows Server 2003

The .NET Framework and .NET Compact Framework do not support all versions of every platform. For a list of the supported versions, see .NET Framework System Requirements.

.NET Framework

Supported in: 3.5

Community Additions

ADD
Show:
© 2014 Microsoft