Asp.Net 中的秒表定时器
我想使用 Asp.Net C# 显示秒表计时器...
我已经浏览了很多线程并也在 Google 上进行了搜索,但我找不到合适的代码或解决方案。
我发现的大多数解决方案都很难实施,其中一些根本不起作用。
我想创建如下图所示的秒表。
通过单击开始按钮,手表会以 HH.MM 的格式从 00.00 开始计时,并更改停止按钮的文本。
通过再次单击该按钮,它会停止手表,然后将该时间保存在数据库中,并将其显示在给定的文本框中。
单击“重置”会将时间重置为 00:00。
我该怎么做?
我对此没有任何想法,所以需要一些代码。
下面是我在网站中尝试过的代码和图像...
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...
I wanna diplay a Stopwatch timer... using Asp.Net C#...
I have looked through many threads and searched on Google also, but I can't find a proper code or solution.
Most of the solutions I found were very hard to implement and some of them didn't work.
I want to create stopwatch like below Image.
By clicking on the start button it starts the watch from 00.00 in the format of HH.MM and and change the text of the button to Stop.
By clicking on that again it stops the watch and then saves that time in a database and also displays it in given TextBox.
By clicking on reset it resets the time to 00.00.
How can i do this?
I doesn't have any idea about this, so some code is needed.
BELOW IS THE CODE AND IMAGE WHICH I TRIED IN THE WEBSITE...
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;
}
}
AND THE CODE WRITTEN IN THE PROJECT (WHICH WORKS WELL) IS...
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)
{
}
}
}
In both the timer interval is set at 1000.
In the website the time is not increasing, it remains at 0:00:00...
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
尝试下面的代码。这对我有用。
在 web 源代码中添加以下代码:
在您的 cs 文件中添加以下源代码:
Try the below code. It works for me.
Add the below code in websource code:
Add following source code in your cs file:
桌面应用程序和 Web 应用程序的开发存在很大差异。
在您的 WinForm 应用程序中,该应用程序在一台电脑上的一个进程中运行。后面的代码可以不断访问用户界面并可以更新当前时间。
在Web 应用程序中,一切都不同了。网络本质上是一个断开连接、无状态的环境。这意味着服务器从世界上某个地方的浏览器获取请求,创建响应并将其发送回浏览器,然后忘记它。
这意味着浏览器中显示的用户界面与服务器上运行的代码之间没有永久连接。这就是您的代码不起作用的原因。
ASP.NET WebForms 的功能可以帮助您解决 Web 开发固有的问题,它使您相信存在某种永久连接。
我建议,为了更好地理解事情是如何工作的,从后面的代码中删除计时器并添加一个按钮,单击该按钮将更改标签文本。
要真正创建秒表,您需要在用户的浏览器中运行的代码,而不是在服务器上运行。在浏览器中运行的代码了解环境和用户界面,并且可以相应地更新内容。这是通过另一种名为 JavaScript 的编程语言完成的。
我建议从此处的教程开始。
There is a big difference in developing for desktop and for web applications.
In your WinForm app, the app is running on one pc, in one process. The code behind has constant access to the User Interface and can update the current time.
In a Web application, everything is different. The web is by it's very nature a disconnected, stateless environment. This means that the server gets a request from a browser somewhere in the world, creates a response and sends this back to the browser and forgets about it.
This means that there is no permanent connection between the user interface showing in the browser and the code running on your on server. This is why your code is not working.
ASP.NET WebForms has functionality that helps you with the problems that are inherent to web development, it kinds of tricks in you believing there is some permanent connection.
I would suggest, to get better understand how things work, to remove the timer from your code behind and add a button that when clicked will change the label text.
To really create a stopwatch you need code that runs in the browser of your users, instead of on the server. Code running in the browser knows about the environment and user interface and it can update things accordingly. This is done in a different programming language called JavaScript.
I would suggest starting with the tutorials here.
您似乎完全混淆了单层有状态桌面应用程序和两层无状态 Web 应用程序。您不能只在服务器上运行代码,然后像 Windows 窗体一样神奇地更新,而无需使用大量复杂的 ajax 和 jquery 技巧。请阅读有关 HTTP、Web 服务器和 Web 浏览器实际工作原理的更多信息。
You appear to be completely confused between single-tier stateful desktop applications, and two-tier stateless web applications. You can't just have code running on a server that will magically update like it's a windows form without a lot of complex ajax and jquery tricks. Please read more about how HTTP, web servers and web browsers actually work.