Performs asynchronous or synchronous Web page postbacks at a defined interval.
Assembly: System.Web.Extensions (in System.Web.Extensions.dll)
'Declaration <AspNetHostingPermissionAttribute(SecurityAction.InheritanceDemand, Level := AspNetHostingPermissionLevel.Minimal)> _ <AspNetHostingPermissionAttribute(SecurityAction.LinkDemand, Level := AspNetHostingPermissionLevel.Minimal)> _ Public Class Timer _ Inherits Control _ Implements IPostBackEventHandler, IScriptControl 'Usage Dim instance As Timer
The control enables you to perform postbacks at a specified interval. When you use the 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 control.
You use the 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 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 control.
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 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).
Adding a control with a small value in the Interval property can generate significant traffic to and from the Web server. Use the 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 control updates the content in the UpdatePanel control every 10 seconds.
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="VB" AutoEventWireup="true" %> <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"> <html xmlns="http://www.w3.org/1999/xhtml" > <head runat="server"> <title>Timer Example Page</title> <script runat="server"> Protected Sub Page_Load(ByVal sender As Object, ByVal e As EventArgs) OriginalTime.Text = DateTime.Now.ToLongTimeString() End Sub Protected Sub Timer1_Tick(ByVal sender As Object, ByVal e As EventArgs) StockPrice.Text = GetStockPrice() TimeOfPrice.Text = DateTime.Now.ToLongTimeString() End Sub Private Function GetStockPrice() As String Dim randomStockPrice As Double = 50 + New Random().NextDouble() Return randomStockPrice.ToString("C") End Function </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>
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.