简体   繁体   English

如何在ASP.NET C#中插入倒数计时器

[英]how to insert countdown timer in asp.net c#

hii I want to insert a countdown timer in my project. 嗨,我想在我的项目中插入一个倒数计时器。 right now i am using the following code: 现在我正在使用以下代码:

{

    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("~/Online Exam/result2.aspx");
    }

the code works fine but when we move to some other page and then return back to main page the timer gets restarted. 代码工作正常,但是当我们移至其他页面然后返回首页时,计时器将重新启动。 How can i overcome with this? 我该如何克服? Please help 请帮忙

It looks like you're resetting the end time on each page load, probably by doing something like: 看起来您正在重置每次页面加载的结束时间,可能是通过执行以下操作:

protected void Page_Load(object sender, EventArgs e)
{
    DateTime start_time = DateTime.Now;
    DateTime end_time = start_time.AddMinutes(15);
    Session["end_t"] = end_time;
}

Instead, you should store the end time only if the timer is not already running: 相反,只有在计时器尚未运行时,才应存储结束时间:

protected void Page_Load(object sender, EventArgs e)
{
    if (Session["end_t"] == null) {
        DateTime start_time = DateTime.Now;
        DateTime end_time = start_time.AddMinutes(15);
        Session["end_t"] = end_time;
    }
}

Make a Master page and make your operations with timer there. 制作一个母版页,并在那里使用计时器进行操作。
or 要么
You can send the counter of timer to next page then do same operations with timer there 您可以将计时器的计数器发送到下一页,然后在那里使用计时器进行相同的操作

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM