ASP.NET Ajax Timer

The Timer control in ASP.NET serves as a trigger for a designated area of the page that is encompassed by an UpdatePanel control. It enables the execution of postbacks at specified intervals, allowing for the automatic refreshment of content. The Interval property of the Timer control is measured in milliseconds, providing the ability to define the time interval between each postback. For instance, setting the Interval property to 5000 milliseconds will refresh the associated UpdatePanel control every 5 seconds.

UpdatePanel control

Consider the following ASP.NET program, where a Timer control and label controls are positioned within the UpdatePanel control area, while an additional label control resides outside the UpdatePanel control area. Upon running the program, the first label initially displays the current server time as the page is loaded. After a five-second interval, the second label located within the UpdatePanel control updates to reflect the current server time. Subsequently, every five seconds, only the second label undergoes updates, illustrating the occurrence of partial-page updating at defined intervals.


<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" ""> <html xmlns=""> <head runat="server"> <title>Untitled Page</title> </head> <body> <form id="form1" runat="server"> <div> <asp:Label ID="Label1" runat="server" Text="Label"></asp:Label> <br /><br /> <asp:ScriptManager ID="ScriptManager1" runat="server"/> <asp:Timer ID="Timer1" runat="server" interval="5000" ontick="Timer1_Tick" /> <asp:UpdatePanel ID="UpdatePanel1" runat="server"> <ContentTemplate> <asp:Label ID="Label2" runat="server" Text="Label"></asp:Label> </ContentTemplate> <Triggers> <asp:AsyncPostBackTrigger controlid="Timer1" eventname="Tick" /> </Triggers> </asp:UpdatePanel> </div> </form> </body> </html>

Timer control

By exploring the Timer control, developers can automate the refreshing of content within a designated section of the web page. This allows for the seamless update of specific information without requiring manual user interaction. The Timer control enhances the responsiveness and interactivity of web applications, ensuring that time-sensitive data is always up-to-date.

Full Source | C#
using System; using System.Web; public partial class _Default : System.Web.UI.Page { protected void Page_Load(object sender, EventArgs e) { Label1.Text = "Page loaded time : " + DateTime.Now.ToString(); } protected void Timer1_Tick(object sender, EventArgs e) { Label2.Text = "Current Server time : " + DateTime.Now.ToString(); } }
Full Source | VB.NET
Partial Class _Default Inherits System.Web.UI.Page Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load Label1.Text = "Page loaded time : " + DateTime.Now.ToString() End Sub Protected Sub Timer1_Tick(ByVal sender As Object, ByVal e As System.EventArgs) Handles Timer1.Tick Label2.Text = "Current Server time : " + DateTime.Now.ToString() End Sub End Class


The Timer control in ASP.NET acts as a catalyst for postbacks within an UpdatePanel control, triggering automatic updates at specified time intervals. By incorporating the Timer control and associating it with the appropriate UpdatePanel control, developers can facilitate partial-page updates, improving the user experience and ensuring that time-dependent data remains current.