I had inserted a countdown timer in my project. When I run it , the timer start but when I stop debugging it and after some time when I again run it then what I notice is, the timer is still running. For example, I have given time of 3 minutes to the timer i.e. after 3 minutes it should redirect to the result page...now I run the page and the count down timer is started...when I stop debugging it suppose at 00:02:45(time left) and after 5 seconds when I again run it then the timer continues with 00:02:40(time left) . I don't want like this to be happen . How do I stop the continuation of the timer? Have a look at my code. Show me where I am making mistake and what is the solution.

On the TimerTest.aspx.cs page, I have the following lines of code:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Data.SqlClient;
using System.Data;
using System.Configuration;

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

    if (Session["end_t"] == null)
        DateTime start_time = DateTime.Now;
        DateTime end_time = start_time.AddMinutes(3);
        Session["end_t"] = end_time;

protected void timer1_tick(object sender, EventArgs e)
    DateTime dt = (DateTime)Session["end_t"];
    DateTime dt_curr = DateTime.Now;
    TimeSpan ts = dt - dt_curr;
    lblTimer.Text = ts.Hours.ToString() + ":" + ts.Minutes.ToString() + ":" + ts.Seconds.ToString();
    if (ts.Minutes == 0)
        timer1.Enabled = false;


On the TimerTest.aspx page, I have inserted the following lines of code:

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

<!DOCTYPE html>

<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">

    <form id="form1" runat="server">
        <asp:ScriptManager ID= "SM1" runat="server"></asp:ScriptManager>
        <asp:Timer ID="timer1" runat="server" Interval="1000" OnTick="timer1_tick"></asp:Timer>
       <asp:UpdatePanel id="updPnl" runat="server" UpdateMode="Conditional">
       <asp:Label ID="lblTimer" runat="server"></asp:Label>
       <asp:AsyncPostBackTrigger ControlID="timer1" EventName ="tick" />

Related posts

Recent Viewed