Count Down timer implementation in ASP.NET
Here I am going to explain, how we can implement simple count down timer in ASP.NET. This can be used in any webpage like if we have around 200 minutes to get live a webpage. we can show this duration using count down timer to our user. This implementation is very simple.
Here i am going to explain, how we can implement a simple count down timer in ASP.NET.
This can be used in any webpage like if we have around 200 minutes to get live a webpage. we can show this duration using count down timer to our user.
This implementation is very simple. This can be achieved using ASP.NET time control. In the code i have explained how to show remaining time in minutes, you can similarly do in hrs/days etc.
Use an script manager to support the timer functionality as below-
<asp:ScriptManager ID= "ScriptManager1" runat="server">
</asp:ScriptManager>
Now use an ASP.NET timer which will show the count down-
<asp:Timer ID="timerformin" runat="server" Interval="1000" OnTick="timer1_tick">
</asp:Timer>
Define a label to show the remaining time.
<asp:Label ID="lbltime" runat="server">
</asp:Label>
In the page load event code behind page , I have created a session variable and ensure that the value of the session variable doesn't get updated due to the async post back caused by the AsyncPostBackTrigger.
With this, you will have the design page like below-
<%@ Page Language="C#" AutoEventWireup="true" CodeFile="Countdowntimer .aspx.cs" Inherits="Countdowntimer_" %>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
<title>COUNT DOWN TIMER IMPLEMENTATION</title>
</head>
<body>
<form id="form1" runat="server">
<div>
<asp:ScriptManager ID= "ScriptManager1" runat="server"></asp:ScriptManager>
<asp:Timer ID="timerformin" runat="server" Interval="1000" OnTick="timer1_tick"></asp:Timer>
</div>
<div>
<asp:UpdatePanel id="uptimer" runat="server" UpdateMode="Conditional">
<ContentTemplate>
<table width="800px">
<tr style="background-color:Silver; border:Aqua; border-width:2px;">
<td align="center" style="border-color:Aqua; border-width:2px;">
<asp:Label ID="lbltime" runat="server"></asp:Label>Minutes
</td>
</tr>
</table>
</ContentTemplate>
<Triggers>
<asp:AsyncPostBackTrigger ControlID="timerformin" EventName ="tick" />
</Triggers>
</asp:UpdatePanel>
</div>
</form>
</body>
</html>
I have used here 160 minutes as the time, you can change it as per your need.
And the code behind page-
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
public partial class Countdowntimer_ : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
if (!ScriptManager1.IsInAsyncPostBack)
Session["timeleft"] = DateTime.Now.AddMinutes(160).ToString();
}
protected void timer1_tick(object sender, EventArgs e)
{
if (0 > DateTime.Compare(DateTime.Now,
DateTime.Parse(Session["timeleft"].ToString())))
{
lbltime.Text = "Number Of Minutes Left To Get Live: " +
((Int32)DateTime.Parse(Session["timeleft"].
ToString()).Subtract(DateTime.Now).TotalMinutes).ToString();
}
}
}
Now run the code and you will see the output as this much minute is left. The output will look like the below image. Don't forget to download the CSS and javascript here. Put the appropriate CSS and javascript in your code.
<img src='/attachments/Resources/45793-133711-Result-of-count-down-timer.png' alt='Result of count down timer' style='padding:10px' >