简体   繁体   中英

Why is the code in Timer Control executed only once?

I have written the following Code:

public partial class Default2 : System.Web.UI.Page
{
    int time = 60;

    protected void Page_Load(object sender, EventArgs e)
    {
        Label2.Text = "" + time; 
    }
    protected void Timer2_Tick(object sender, EventArgs e)
    {
        time = time - 1;
        Label2.Text = "" + time;
    }
}

I am trying something similar to a Countdown Timer. The Output should be printed as '60' then '59' then '58' and so on. But the code in the timer gets executed only once ie the output is printed as '60' and then '59' and then the number stops decrementing.

The timer is executed each second. But int time is not static, it will start is 60 each time the timer is executed. Try putting DateTime.Now into the label text and you will see that the Timer is not the issue.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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