title: Maintaining state of ajax based countdown timer in asp.net
body: <p>I have created a countdown timer with the help of a code on internet by Member 4332221 on Code Project with some updates in it and I am using this timer on a project for online testing </p>
  
  <p>Following is the code</p>
  
  <p>aspx code</p>
  
  <pre><code>&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;
  </code></pre>
  
  <p>aspx.cs</p>
  
  <pre><code>protected void Page_Load(object sender, EventArgs e)
  {
      if (!SM1.IsInAsyncPostBack)
          Session["timeout"] = DateTime.Now.AddMinutes(1).ToString();
  }
  
  protected void timer1_tick(object sender, EventArgs e)
  {
      if (0 &gt; DateTime.Compare(DateTime.Now,
      DateTime.Parse(Session["timeout"].ToString())))
      {
          int hrs = (((Int32)DateTime.Parse(Session["timeout"].
          ToString()).Subtract(DateTime.Now).TotalMinutes)) / 60;
  
          int mins = (((Int32)DateTime.Parse(Session["timeout"].
          ToString()).Subtract(DateTime.Now).TotalMinutes)) % 60;
  
          int seconds = (((Int32)DateTime.Parse(Session["timeout"].
          ToString()).Subtract(DateTime.Now).TotalSeconds))%60;
  
  
          lblTimer.Text = "Time left is " + hrs.ToString() + " : " + mins.ToString() + " : " + seconds.ToString();
  
          if (hrs == 0 &amp;&amp; mins == 0 &amp;&amp; seconds == 0)
          {
              lblTimer.Text = "Test Time Over";
          }
      }
  }
  </code></pre>
  
  <p>This code is working properly if I am running it alone but the problem is that if I apply this in my module where I want to start this on button click event it works only once and while clicking on any other button the timer goes back to initial position.</p>
  
  <p>I tried to apply <code>Postback = false</code> but then the timer is not displaying.</p>
  
  <p>How can I load this timer on button click and maintain its state on click event of other controls  </p>