Timer Example in ASP.NET using C#

Timer Example in ASP.NET using C#


Posted in : ASP.Net Posted on : December 15, 2010 at 6:08 PM Comments : [ 0 ]

In this article we will introduce with Timer control in ASP.NET using C#.

Timer Example in ASP.NET using C#

Timer control in ASP.NET is a AJAX control. The Timer use the Tick event for show the time. You will set the Interval of timer. In this example we use the UpdatePanel, Timer and Label. Label shows the current time in interval.

Timer.aspx (Design Page):

Timer.aspx (source code):

<%@ Page Title="" Language="C#" MasterPageFile="~/RoseindiaMaster.master" AutoEventWireup="true"
CodeFile="Timer.aspx.cs" Inherits="Timer" %>
<asp:Content ID="Content1" ContentPlaceHolderID="head" runat="Server">
</asp:Content>
<asp:Content ID="Content2" ContentPlaceHolderID="ContentPlaceHolder1" runat="Server">
<div>
<h2 style="color: Green">
Timer in ASP.NET 4, C#</h2>
<asp:ScriptManager ID="ScriptManager1" runat="server">
</asp:ScriptManager>
<asp:Timer ID="Timer1" runat="server" Interval="60" OnTick="Timer1_Tick">
</asp:Timer>
<br />
<asp:UpdatePanel ID="UpdatePanel1" runat="server">
<ContentTemplate>
<asp:Label runat="server" ID="Label1" Font-Bold="True" 
Font-Names="Book Antiqua" Font-Size="XX-Large"
ForeColor="#003300" />
</ContentTemplate>
<Triggers>
<asp:AsyncPostBackTrigger ControlID="Timer1" EventName="Tick" />
</Triggers>
</asp:UpdatePanel>
</div>
</asp:Content>

Timer.aspx.cs (C# code file):

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
public partial class Timer : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
Label1.Text = System.DateTime.Now.ToLongTimeString();
}
private void UpdateTimer()
{
Label1.Text = System.DateTime.Now.ToLongTimeString();
}
protected void Timer1_Tick(object sender, EventArgs e)
{
UpdateTimer();
}
}

Output:

Download source code
Go to Topic «PreviousHomeNext»

Your Comment:


Your Name (*) :
Your Email :
Subject (*):
Your Comment (*):
  Reload Image
 
 

 
Tutorial Topics