AJAX Timer control

The article explains the use of ajax timer control in Asp.Net. The ASP.Net AJAX Timer control will be used to automatically refresh ASP.Net AJAX Update Panel at regular intervals of time.

Add below code in aspx page of empty web application. Timer control is placed 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.

<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
    <title></title>
</head>
<body>
    <form id="form1" runat="server">
        <div>
            <h2>AJAX Timer control </h2>
            <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>
        </div>
    </form>
</body>
</html>

 C# Code

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");
        }

 Output

Demo Code

Download Demo