[英]ASP.Net C# - Display Stopwatch
I currently have a page (page.aspx) that includes a Stopwatch functionality to time how long a user spends on the page over a number of post backs as shown below. 我目前有一个页面(page.aspx),其中包含秒表功能,用于计时用户在许多回发中花费在页面上的时间,如下所示。
However, this is completely invisible to the user. 但是,这对于用户是完全不可见的。 How could I display on the page the Stopwatch and each incremental second until it stops?
如何在秒表页面上显示秒表,并显示秒数直到停止?
protected void Page_Load(object sender, EventArgs e)
{
sw = new Stopwatch();
sw.Start();
}
protected void btnNext_Click(object sender, EventArgs e)
{
//if not at the end of the counter
{
//code
}
else
{
sw.Stop();
}
}
Try something like this: 尝试这样的事情:
.aspx page .aspx页面
<form id="form1" runat="server">
<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" />
<div>
<asp:Button ID="Button1" Text="next" OnClick="Button1_OnClick" runat="server" />
</div>
</ContentTemplate>
<Triggers>
<asp:AsyncPostBackTrigger ControlID="tm1" EventName="Tick" />
</Triggers>
</asp:UpdatePanel>
</form>
Server side Code: 服务器端代码:
public static Stopwatch sw;
protected void Page_Load(object sender, EventArgs e)
{
if (!IsPostBack)
{
sw = new Stopwatch();
sw.Start();
}
}
protected void Button1_OnClick(object sender, EventArgs e)
{
}
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");
}
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.