ASP.Net c# -显示秒表
本文关键字:显示 Net ASP | 更新日期: 2023-09-27 18:18:24
我目前有一个页面(page.aspx),其中包含一个Stopwatch功能,用于记录用户在页面上花费的时间,如下图所示。
但是,这对用户来说是完全不可见的。我如何在页面上显示秒表和每一秒的增量,直到它停止?
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();
}
}
试试这样:
。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>
服务器端代码:
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");
}
}