How to use Ajax Timer control in asp.net?



Hi
If our requirement to update some portion of page on basis of some time interval, then we can use “Ajax Timer” control. Here I m updating the system date on some time interval (2 sec).

We can do like this

Step1: Take ScriptManager,Timer,UpdatePanel, Label control and arrange the code like this

<%@ Page Language="C#" AutoEventWireup="true" CodeFile="TimerControl.aspx.cs" Inherits="TimerControl" %>

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"&gt;

<html xmlns="http://www.w3.org/1999/xhtml"&gt;
<head runat="server">
<title></title>
</head>
<body>
<form id="form1" runat="server">
<div>

<asp:ScriptManager ID="ScriptManager1" runat="server"/>

<asp:Timer ID="Timer1" Interval="2000" runat="server" ontick="Timer1_Tick"/>

<asp:UpdatePanel ID="UpdatePanel1" runat="server">
<Triggers>
<asp:AsyncPostBackTrigger ControlID="Timer1" EventName="Tick" />

</Triggers>

<ContentTemplate>

<p>Date will update on interval of 2 secounds</p>

<asp:Label ID="LblDate" runat="server" Font-Bold="true" ForeColor="Red" Text=""></asp:Label>

</ContentTemplate>
</asp:UpdatePanel>

</div>
</form>
</body>
</html>

Step2:Write the code in code behind page like this

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;

public partial class TimerControl : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{

}
protected void Timer1_Tick(object sender, EventArgs e)
{

LblDate.Text = System.DateTime.Now.ToString();

}
}

Advertisement

2 thoughts on “How to use Ajax Timer control in asp.net?

    • Chandra Dev January 9, 2012 / 5:20 pm

      Thanks for sharing the URL and you are welcome.I m glad to hear nice feedback from you.

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s

This site uses Akismet to reduce spam. Learn how your comment data is processed.