.NET Tutorials, Forums, Interview Questions And Answers
Welcome :Guest
 
Sign In
Register
 
Win Surprise Gifts!!!
Congratulations!!!


Top 5 Contributors of the Month
david stephan

Home >> Articles >> ASP.NET >> Post New Resource Bookmark and Share   

 Subscribe to Articles

Implementation of Count Down timer in ASP.NET Applications

Posted By:Ashutosh Jha       Posted Date: July 26, 2014    Points: 200    Category: ASP.NET    URL: http://www.dotnetspark.com  

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 600 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></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.

 Subscribe to Articles

     

Further Readings:

Responses

No response found. Be the first to respond this post

Post Comment

You must Sign In To post reply
Find More Articles on C#, ASP.Net, Vb.Net, SQL Server and more Here

Hall of Fame    Twitter   Terms of Service    Privacy Policy    Contact Us    Archives   Tell A Friend