asp.net按钮单击计时器无法启动

时间:2016-07-22 05:37:24

标签: asp.net

我想在按钮点击时运行timer_Tick事件,并按下另一个按钮停止计时器。这是我的代码

protected void Button1_Click(object sender, EventArgs e)
        {
           Timer1.Enabled = true;
        }
protected void Timer1_Tick(object sender, EventArgs e)
        {
            Label2.Text = "Timer started";
            //string png = IpAddress.Text.ToString(); //"192.168.153.12";// 
            if (IpAddress.Text == "" || IpAddress.Text == null)
            {
                Response.Write("Error");
            }
            else
            {
                ping1(IpAddress.Text);
                ping2(Ipaddres1.Text);

            }
        }

1 个答案:

答案 0 :(得分:0)

使用更新面板并编写代码。

 <asp:ScriptManager ID="ScriptManager1" runat="server">
 </asp:ScriptManager>
 <asp:Button ID="Button1" runat="server" Text="Button" 
    onclick="Button1_Click1" />

 <asp:Timer ID="Timer1" runat="server" ontick="Timer1_Tick" Interval="2000"    Enabled="False">
 </asp:Timer>
 <asp:UpdatePanel ID="UpdatePanel1" runat="server">
       <ContentTemplate>
           <asp:Label ID="Label1" runat="server" Text="Label"></asp:Label>
           <br />
          <asp:Label ID="Label2" runat="server" Text="Label"></asp:Label>
       </ContentTemplate>
           <Triggers>
               <asp:AsyncPostBackTrigger ControlID="Timer1" EventName="Tick" />
           </Triggers>
 </asp:UpdatePanel>

代码背后,

 protected void Button1_Click(object sender, EventArgs e)
    {
       Timer1.Enabled = true;
    }

protected void Timer1_Tick(object sender, EventArgs e)
{
    Label1.Text = "Timer started";
        //string png = IpAddress.Text.ToString(); //"192.168.153.12";// 
        if (IpAddress.Text == "" || IpAddress.Text == null)
        {
           Label2.Text = "Error!!!";
        }
        else
        {
            ping1(IpAddress.Text);
            ping2(Ipaddres1.Text);

        }

        }