title: How to make a countdown timer in asp.net web application(C#)?
body: <p>I am creating timed online test in C# asp.net. I have created it using the local machine time. But in the case of Database disconnection or system hang, The countdown timer must start from the time at which the machine has hanged or unexpected database disconnectivity. For example the user is answering the Questions for 10 mins and unexpectedly system hanged. After recovery the countdown must start from 10 mins. Can anyone please help me with coding in C#? </p>
  
  <p>I have used the following code, But its not useful in the above scenario. </p>
  
  <pre><code>int totalTime = (Convert.ToInt32(ViewState["totaltime"])) * 60;
  
  DateTime startTime = (DateTime)ViewState["startTime"];
  TimeSpan elaspedTime = DateTime.Now.Subtract(startTime);
  Literal1.Text = elaspedTime.Hours.ToString() + "h" + ":" + elaspedTime.Minutes.ToString() + 
      "m" + ":" + elaspedTime.Seconds.ToString() + "s";
  
  int finish = Convert.ToInt32(elaspedTime.TotalSeconds);
  
  int remaingsec = (totalTime - finish);
  TimeSpan remainingtime = TimeSpan.FromSeconds(remaingsec);
  string answer = string.Format("{0:D2}h:{1:D2}m:{2:D2}s",
                      remainingtime.Hours,
                      remainingtime.Minutes,
                      remainingtime.Seconds,
                      remainingtime.Milliseconds);
  
  LIteral2.Text =  answer;
  if (totalTime == finish)
  {
       lnkFinish_Click(sender, e);
       Response.Redirect(@"~/Error.aspx");
  }
  </code></pre>