In this article I will explain a tutorial with an example, how to use the ASP.Net AJAX Timer control.
The ASP.Net AJAX Timer control will be used to automatically refresh ASP.Net AJAX UpdatePanel at regular intervals.
HTML Markup
I have placed a Timer control inside the AJAX UpdatePanel and its Interval has been set to 1000 milliseconds i.e. one second.
Thus now every second the Timer executes its OnTick event which in turns refreshes the UpdatePanel and thus the Label is set with the current time.
<asp:ScriptManager ID="ScriptManager1" runat="server">
</asp:ScriptManager>
<asp:UpdatePanel ID="UpdatePanel1" runat="server">
<ContentTemplate>
<asp:Label ID="lblTime" runat="server" />
<asp:Timer ID="Timer1" runat="server" OnTick="GetTime" Interval="1000" />
</ContentTemplate>
</asp:UpdatePanel>
C#
protected void Page_Load(object sender, EventArgs e)
{
if (!this.IsPostBack)
{
lblTime.Text = DateTime.Now.ToString("hh:mm:ss tt");
}
}
protected void GetTime(object sender, EventArgs e)
{
lblTime.Text = DateTime.Now.ToString("hh:mm:ss tt");
}
VB.Net
Protected Sub Page_Load(sender As Object, e As EventArgs) Handles Me.Load
If Not Me.IsPostBack Then
lblTime.Text = DateTime.Now.ToString("hh:mm:ss tt")
End If
End Sub
Protected Sub GetTime(sender As Object, e As EventArgs)
lblTime.Text = DateTime.Now.ToString("hh:mm:ss tt")
End Sub
Demo
Downloads