Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/297.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# ASP.NETC-显示秒表_C#_Asp.net_Timer_Stopwatch - Fatal编程技术网

C# ASP.NETC-显示秒表

C# ASP.NETC-显示秒表,c#,asp.net,timer,stopwatch,C#,Asp.net,Timer,Stopwatch,我目前有一个page.aspx页面,它包含一个秒表功能,用于计算用户在页面上花费的时间,如下图所示 但是,这对用户来说是完全不可见的。如何在页面上显示秒表和每一秒的增量,直到它停止 protected void Page_Load(object sender, EventArgs e) { sw = new Stopwatch(); sw.Start(); } protected void btnNext_Click(object sender, EventArgs e) {

我目前有一个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();
    }
}

试着这样做:

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