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();

}
}

Advertisements
Posted in Ajax. 2 Comments »

2 Responses to “How to use Ajax Timer control in asp.net?”

  1. Ankit Singh Says:

    This is best one article so far I have read online. I would like to appreciate you for making it very simple and easy. I have found another nice post related to this post over the internet which also explained very well. For more details you may check it by visiting this url……..
    http://mindstick.com/Articles/385ebacd-89e2-40d3-868a-0185e7c45f15/?ASP.Net%20AJAX%20Server%20Control:%20Timer

    Thanks

    • Chandra Dev Says:

      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 )

Google+ photo

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

Connecting to %s

%d bloggers like this: