Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/asp.net/29.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Asp.net 如何创建秒表计时器_Asp.net_Timer_Stopwatch - Fatal编程技术网

Asp.net 如何创建秒表计时器

Asp.net 如何创建秒表计时器,asp.net,timer,stopwatch,Asp.net,Timer,Stopwatch,我有一个asp.net应用程序,应该像秒表计时器一样工作。我已经尝试过如何让计时器用秒来计算时间。我使用的算法效率很低。然后,我尝试使用datetime数学使用timespan对象,但由于这基本上是一个时钟,所以它是不受欢迎的。现在我正在尝试实现System.Diagnostics,以便使用秒表对象。当我运行程序时,应该随时间更新的标签只显示所有0 这是带有计时器和更新面板的.aspx页面中的代码: <asp:UpdatePanel ID="UpdatePanel1" runa

我有一个asp.net应用程序,应该像秒表计时器一样工作。我已经尝试过如何让计时器用秒来计算时间。我使用的算法效率很低。然后,我尝试使用datetime数学使用timespan对象,但由于这基本上是一个时钟,所以它是不受欢迎的。现在我正在尝试实现System.Diagnostics,以便使用秒表对象。当我运行程序时,应该随时间更新的标签只显示所有0

这是带有计时器和更新面板的.aspx页面中的代码:

      <asp:UpdatePanel ID="UpdatePanel1" runat="server">
                    <ContentTemplate>
                        <asp:Timer ID="Timer1" runat="server" Enabled="false" 
                            Interval="1000" OnTick="Timer1_Tick"></asp:Timer>
                        <asp:Label ID="Label1" runat="server"></asp:Label>
                    </ContentTemplate>
                </asp:UpdatePanel>
这是timer1的事件(currentTime是timespan对象):


我真的不知道我做错了什么。我认为这是制作秒表计时器最简单的方法,但标签内没有任何变化。我将感谢任何帮助。如有必要,我将提供更多信息。

请尝试以下代码。它对我有用

在websource代码中添加以下代码:

<asp:scriptmanager ID="Scriptmanager1" runat="server"></asp:scriptmanager>
<asp:UpdatePanel ID="UpdatePanel1" runat="server">
  <ContentTemplate>
    <asp:Label ID="Label1" runat="server" Font-Size="XX-Large"></asp:Label>
    <asp:Timer ID="tm1" Interval="1000" runat="server" ontick="tm1_Tick" />
  </ContentTemplate>
  <Triggers>
    <asp:AsyncPostBackTrigger ControlID="tm1" EventName="Tick" />
  </Triggers>
</asp:UpdatePanel>

在cs文件中添加以下源代码:

using System.Diagnostics;

public partial class ExamPaper : System.Web.UI.Page
{
    public static Stopwatch sw;

    protected void Page_Load(object sender, EventArgs e)
    {
        if (!IsPostBack)
        {
            sw = new Stopwatch();
            sw.Start();
        }
    }

    protected void tm1_Tick(object sender, EventArgs e)
    {
        long sec = sw.Elapsed.Seconds;
        long min = sw.Elapsed.Minutes;

        if (min < 60)
        {
            if (min < 10)
                Label1.Text = "0" + min;
            else
                Label1.Text = min.ToString();

            Label1.Text += " : ";

            if (sec < 10)
                Label1.Text += "0" + sec;
            else
                Label1.Text += sec.ToString();
        }
        else
        {
            sw.Stop();
            Response.Redirect("Timeout.aspx");
        }
    }
} 
使用系统诊断;
公共部分类示例:System.Web.UI.Page
{
公共静态秒表sw;
受保护的无效页面加载(对象发送方、事件参数e)
{
如果(!IsPostBack)
{
sw=新秒表();
sw.Start();
}
}
受保护的无效tm1_勾号(对象发送方、事件参数e)
{
长秒=短波经过的秒数;
长分钟=短波经过的分钟数;
如果(最小值<60)
{
如果(最小值<10)
标签1.Text=“0”+最小值;
其他的
Label1.Text=min.ToString();
标签1.Text+=“:”;
如果(第10节)
标签1.文本+=“0”+秒;
其他的
Label1.Text+=sec.ToString();
}
其他的
{
sw.Stop();
重定向(“Timeout.aspx”);
}
}
} 

查看ASP.Net代码在服务器上运行是否对您有好处,您正在尝试查看浏览器中的更改-因此,正如预期的那样,您将获得静态0。很可能Steve的链接(客户端计时器)是您所需要的-否则请评论您想要实现的目标。我只是想制作一个秒表计时器,我希望我可以使用CodeBehind文件来实现这一点,但我想我是被迫使用JavaScript来实现的。
<asp:scriptmanager ID="Scriptmanager1" runat="server"></asp:scriptmanager>
<asp:UpdatePanel ID="UpdatePanel1" runat="server">
  <ContentTemplate>
    <asp:Label ID="Label1" runat="server" Font-Size="XX-Large"></asp:Label>
    <asp:Timer ID="tm1" Interval="1000" runat="server" ontick="tm1_Tick" />
  </ContentTemplate>
  <Triggers>
    <asp:AsyncPostBackTrigger ControlID="tm1" EventName="Tick" />
  </Triggers>
</asp:UpdatePanel>
using System.Diagnostics;

public partial class ExamPaper : System.Web.UI.Page
{
    public static Stopwatch sw;

    protected void Page_Load(object sender, EventArgs e)
    {
        if (!IsPostBack)
        {
            sw = new Stopwatch();
            sw.Start();
        }
    }

    protected void tm1_Tick(object sender, EventArgs e)
    {
        long sec = sw.Elapsed.Seconds;
        long min = sw.Elapsed.Minutes;

        if (min < 60)
        {
            if (min < 10)
                Label1.Text = "0" + min;
            else
                Label1.Text = min.ToString();

            Label1.Text += " : ";

            if (sec < 10)
                Label1.Text += "0" + sec;
            else
                Label1.Text += sec.ToString();
        }
        else
        {
            sw.Stop();
            Response.Redirect("Timeout.aspx");
        }
    }
}