加载页面后,我的倒计时器就会开始。但我想要的是计时器应该在单击按钮后启动。
protected void Timer1_Tick(object sender, EventArgs e)
{
if (TimeAllSecondes > 0)
{
TimeAllSecondes = TimeAllSecondes - 1;
}
TimeSpan time_Span = TimeSpan.FromSeconds(TimeAllSecondes);
hh = time_Span.Hours;
mm = time_Span.Minutes;
ss = time_Span.Seconds;
Label2.Text = " " + hh + ":" + mm + ":" + ss;
}
protected void Button1_Click(object sender, EventArgs e)
{
}
最佳答案
在设计时禁用 Timer 控件,例如:
runat="server" Interval="2000" Enabled="False" />
然后在 Button_Click 中启用它:
protected void Button1_Click(object sender, EventArgs e)
{
Timer1.Enabled = true;
}
除非服务器端计时(不受客户端操作影响)很重要,否则最好在 Javascript 中执行此操作。
关于c# - 如何在asp.net C# 中单击按钮后启动倒计时器?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/45582098/