开发者

how to insert countdown timer in asp.net c#

开发者 https://www.devze.com 2023-01-24 17:00 出处:网络
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\"];

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 w开发者_运维百科e 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

0

精彩评论

暂无评论...
验证码 换一张
取 消