Create Count Down Timer using ASP.NET Timer Control and Ajax[
^]
<asp:ScriptManager ID="ScriptManager1" runat="server">
</asp:ScriptManager>
<asp:UpdatePanel ID="UpdatePanel1" runat="server">
<ContentTemplate>
<asp:Timer ID="Timer1" runat="server" Interval="1000" ontick="Timer1_Tick">
</asp:Timer>
<asp:Label ID="Label1" runat="server" Text=""></asp:Label>
</ContentTemplate>
</asp:UpdatePanel>
protected void Page_Load(object sender, EventArgs e)
{
if (Session["CountdownTimer"] == null)
{
Session["CountdownTimer"] = new CountDownTimer(TimeSpan.Parse("2:30:00"));
(Session["CountdownTimer"] as CountDownTimer).Start();
}
}
protected void Timer1_Tick(object sender, EventArgs e)
{
if (Session["CountdownTimer"] != null)
{
Label1.Text = (Session["CountdownTimer"] as CountDownTimer).TimeLeft.ToString();
}
}
public class CountDownTimer
{
public TimeSpan TimeLeft;
System.Threading.Thread thread;
public CountDownTimer(TimeSpan original)
{
this.TimeLeft = original;
}
public void Start()
{
thread = new System.Threading.Thread(() =>
{
while (true)
{
System.Threading.Thread.Sleep(1000);
TimeLeft = TimeLeft.Subtract(TimeSpan.Parse("00:00:01"));
}
});
thread.Start();
}
}
Code is ripped from here[^].
-KR