Asp.Net中的秒表计时器

问题描述 投票:1回答:3

我想用Asp.Net C#播放秒表计时器...

我浏览了许多主题,也在Google上进行了搜索,但找不到合适的代码或解决方案。

我发现的大多数解决方案都很难实施,其中一些无法使用。

我想像下面的图像一样创建秒表。

<< img src =“ https://image.soinside.com/eyJ1cmwiOiAiaHR0cHM6Ly9pLnN0YWNrLmltZ3VyLmNvbS90NjV0ay5wbmcifQ==” alt =“在此处输入图像描述”>

通过单击开始按钮,它以HH.MM格式从00.00开始计时,并将按钮的文本更改为Stop。

再次单击该按钮,它会停止监视,然后将其保存在数据库中,并在给定的TextBox中显示。

通过单击重置将时间重置为00.00。

我该怎么做?

我对此一无所知,因此需要一些代码。

下面是我在网站上尝试过的代码和图像...

<< img src =“ https://image.soinside.com/eyJ1cmwiOiAiaHR0cHM6Ly9pLnN0YWNrLmltZ3VyLmNvbS95NU5kTy5wbmcifQ==” alt =“在此处输入图像描述”>

using System;
using System.Collections;
using System.Configuration;
using System.Data;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Xml.Linq;
using System.Diagnostics;

public partial class Default5 : System.Web.UI.Page
{
    private Stopwatch sw = new Stopwatch();
        protected void Page_Load(object sender, EventArgs e)
        {


        }

        protected void Button3_Click(object sender, EventArgs e)
        {
            if (Button3.Text == "start")
            {
                Timer1.Enabled = true;
                sw.Start();
                Button3.Text = "stop";

            }
            else
            {
                Timer1.Enabled = false;
                sw.Stop();
                Button3.Text = "start";

                //TextBox1.Text = Label1.Text;
            }
        }

        protected void Timer1_Tick(object sender, EventArgs e)
        {
            int hrs = sw.Elapsed.Hours;
            //int hrs = "1;
            int mins = sw.Elapsed.Minutes;
            int secs = sw.Elapsed.Seconds;

            Label1.Text = hrs + ":";

            if (mins < 10)
               Label1.Text += "0" + mins + ":";
            else
                Label1.Text += mins + ":";

            if (secs < 10)
                Label1.Text += "0" + secs;
            else
                Label1.Text += secs;
        }
}

和项目中编写的代码(运作良好)...

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Diagnostics;

namespace WindowsFormsApplication1
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
        }
        private Stopwatch sw = new Stopwatch();

        private void button1_Click(object sender, EventArgs e)
        {
            if (button1.Text == "Start")
            {
                timer1.Enabled = true;
                sw.Start();
                button1.Text = "Stop";

            }
            else
            {
                timer1.Enabled = false;
                sw.Stop();
                button1.Text = "Start";
                textBox1.Text = label1.Text;
            }
        }

        private void timer1_Tick(object sender, EventArgs e)
        {
            int hrs = sw.Elapsed.Hours, mins = sw.Elapsed.Minutes, secs = sw.Elapsed.Seconds;

            label1.Text = hrs + ":";

            if(mins <10)
                label1.Text += "0"  + mins + ":";
            else 
                label1.Text += mins + ":";

            if (secs < 10)
                label1.Text += "0" + secs;
            else
                label1.Text += secs;
        }

        private void Form1_Load(object sender, EventArgs e)
        {

        }
    }
}

两个定时器的间隔都设置为1000。

在网站上,时间没有增加,它保持在0:00:00 ...

asp.net timer stopwatch
3个回答
1
投票

在开发桌面和Web应用程序方面有很大的不同。

WinForm应用中,该应用在一台PC上以一个过程运行。后面的代码可以持续访问用户界面,并且可以更新当前时间。

Web application中,一切都不同。网络本质上是一个无连接的,无状态的环境。这意味着服务器从世界上某个地方的浏览器获取请求,创建响应并将其发送回浏览器,而忽略了它。

这意味着浏览器中显示的用户界面与服务器上运行的代码之间没有永久连接。这就是为什么您的代码无法正常工作的原因。

ASP.NET WebForms具有可帮助您解决Web开发所固有的问题的功能,它使您相信存在某种永久的连接。

我建议,为了更好地了解其工作原理,从后面的代码中删除计时器,并添加一个按钮,单击该按钮将更改标签文本。

要真正创建秒表,您需要在用户浏览器中而不是服务器上运行的代码。在浏览器中运行的代码了解环境和用户界面,并且可以相应地进行更新。这是通过另一种称为JavaScript的编程语言完成的。

我建议从教程here开始。


2
投票

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

在网络源代码中添加以下代码:

<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");
        }
    }
} 

0
投票

您似乎对单层有状态桌面应用程序和两层无状态Web应用程序完全感到困惑。您不能仅仅在运行在服务器上的代码就可以像Windows窗体那样神奇地更新,而无需许多复杂的ajax和jquery技巧。请阅读有关HTTP,Web服务器和Web浏览器实际工作方式的更多信息。

最新问题
© www.soinside.com 2019 - 2024. All rights reserved.