我目前有一个页面(page.aspx),其中包含一个秒表功能,用于计算用户在多个回帖上花费多长时间,如下所示。
但是,这对用户来说完全不可见。如何在页面上显示秒表和每秒增量秒,直到它停止?
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();
}
}
答案 0 :(得分:0)
尝试这样的事情:
.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");
}
}