title: How do I stop countdown timer using asp.net timer control?
body: <p>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. </p>
  
  <p><b>On the TimerTest.aspx.cs page, I have the following lines of code:</b></p>
  
  <pre><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;
          Response.Redirect("/Student/Result.aspx");
      }
  
  }
  }
  </code></pre>
  
  <p><b>On the TimerTest.aspx page, I have inserted the following lines of code:</b></p>
  
  <p><div class="snippet" data-lang="js" data-hide="false">
  <div class="snippet-code">
  <pre class="snippet-code-html lang-html prettyprint-override"><code>&lt;%@ Page Language="C#" AutoEventWireup="true" CodeFile="TimerTest.aspx.cs" Inherits="Student_TimerTest" %&gt;
  
  &lt;!DOCTYPE html&gt;
  
  &lt;html xmlns="http://www.w3.org/1999/xhtml"&gt;
  &lt;head runat="server"&gt;
      &lt;title&gt;&lt;/title&gt;
  
  &lt;/head&gt;
  &lt;body&gt;
      &lt;form id="form1" runat="server"&gt;
      &lt;div&gt;
          &lt;asp:ScriptManager ID= "SM1" runat="server"&gt;&lt;/asp:ScriptManager&gt;
          &lt;asp:Timer ID="timer1" runat="server" Interval="1000" OnTick="timer1_tick"&gt;&lt;/asp:Timer&gt;
          &lt;/div&gt;
     
         &lt;div&gt;
         &lt;asp:UpdatePanel id="updPnl" runat="server" UpdateMode="Conditional"&gt;
         &lt;ContentTemplate&gt;
         &lt;asp:Label ID="lblTimer" runat="server"&gt;&lt;/asp:Label&gt;
         &lt;/ContentTemplate&gt;
         &lt;Triggers&gt;
         &lt;asp:AsyncPostBackTrigger ControlID="timer1" EventName ="tick" /&gt;
         &lt;/Triggers&gt;
         &lt;/asp:UpdatePanel&gt;
  &lt;/div&gt;  
  &lt;/form&gt;
  &lt;/body&gt;
  &lt;/html&gt;</code></pre>
  </div>
  </div>
  </p>