C# 如何使用C将计时器连接到按钮#
我有一个计时器(带标签)和一个按钮在我的一页上。计时器正在使用一个简单的onTickEventC# 如何使用C将计时器连接到按钮#,c#,asp.net,timer,C#,Asp.net,Timer,我有一个计时器(带标签)和一个按钮在我的一页上。计时器正在使用一个简单的onTickEvent protected void Timer1_Tick(object sender, EventArgs e) { int seconds = int.Parse(Label1.Text); if (seconds > 0) Label1.Text = (seconds - 1).ToString(); els
protected void Timer1_Tick(object sender, EventArgs e)
{
int seconds = int.Parse(Label1.Text);
if (seconds > 0)
Label1.Text = (seconds - 1).ToString();
else
Timer1.Enabled = false;
}
所有扩展都分组在一个UpdatePanel中
<asp:ScriptManager ID="ScriptManager1" runat="server"></asp:ScriptManager>
<asp:UpdatePanel ID="UpdatePanel1" runat="server">
<ContentTemplate>
<asp:Button ID="schlbtn1" runat="server" Text="Go To Lectures" CssClass="btn btn-lg btn-success btn-block" OnClick="schlbtn1_Click" Visible="true" ForeColor="Black" ViewStateMode="Enabled" ClientIDMode="Static" />
<asp:Label ID="Label1" runat="server">60</asp:Label>
<asp:Timer ID="Timer1" runat="server" Interval="1000" OnTick="Timer1_Tick">
</asp:Timer>
</ContentTemplate>
</asp:UpdatePanel>
60
逻辑很简单。计时器使用默认设置为60的标签,并在间隔为1000毫秒的每一个刻度上将其递减1。一切正常,但不幸的是,计时器在我加载页面后立即开始倒计时。当我单击ID为“schlbtn1”的按钮时,我想以某种方式连接计时器,使其启动。计时器在创建时默认处于启用状态,因此您需要禁用它,然后手动启用它。您可以按如下方式执行此操作: 在
UpdatePanel
中(注意Enabled
设置为false):
这将导致计时器启动被禁用,然后在按下按钮时启动。您是否尝试过在
UpdatePanel
中的Timer1
上设置Enabled=“false”
,然后在schlbtn1\u单击事件中启用计时器。。。成功了。我没有想到这一点。谢谢大家!@布莱恩,把它作为答案贴出来,让OP接受。
<asp:Timer ID="Timer1" runat="server" Enabled="false" Interval="1000" OnTick="Timer1_Tick"></asp:Timer>
Timer1.Enabled = true;