c#時間倒計時

後臺代碼:
using System;
using System.Data;
using System.Configuration;
using System.Collections;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;

namespace AJAXEnabledWebApplication1
{
    public partial class Timer : System.Web.UI.Page
    {
       
        private DateTime dtExam = DateTime.Parse("2009-07-22 20:42:00"); //定義時間到期時間       

        protected void Page_Load(object sender, EventArgs e)
        {
        }

       protected void timerCD_Tick(object sender, EventArgs e)
        {
            if (this.dtExam < DateTime.Now) //如果設置的時間已過
            {
                this.timerCD.Enabled = false;    //將Timmer置爲false
                labTimes.Text = "時間到!";
            }
            else
            {
                RefreshTime();                 //刷新時間
}
        }

        private void RefreshTime() //刷新時間的方法
        {
            TimeSpan ts = this.dtExam - DateTime.Now; //時間差
            this.labDays.Text = ts.Days.ToString().PadLeft(2, '0') + "天";
            this.labTimes.Text = ts.Hours.ToString().PadLeft(2, '0') + ":" + ts.Minutes.ToString().PadLeft(2, '0') + ":" + ts.Seconds.ToString().PadLeft(2, '0');
        }

    }

}

 
前臺代碼:

<%@ Page Language="C#" AutoEventWireup="true" CodeBehind="Timer.aspx.cs" Inherits="AJAXEnabledWebApplication1.Timer" %>

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">

<html xmlns="http://www.w3.org/1999/xhtml" >
<head runat="server">
    <title>倒計時</title>
</head>
<body>
    <form id="form1" runat="server">
    <div>
        &nbsp;</div>
        &nbsp;<asp:TextBox ID="TextBox3" runat="server"></asp:TextBox>&nbsp;
        <asp:TextBox ID="TextBox1" runat="server"></asp:TextBox>
        時<asp:TextBox ID="TextBox2" runat="server"></asp:TextBox>分<asp:ScriptManager ID="ScriptManager1" runat="server">
        </asp:ScriptManager>
        <asp:UpdatePanel ID="UpdatePanel1" runat="server">
            <ContentTemplate>
        <asp:Timer ID="timerCD" runat="server" Interval="1000" OnTick="timerCD_Tick">
        </asp:Timer>
        <asp:Label ID="labDays" runat="server" Width="155px"></asp:Label><asp:Label ID="labTimes" runat="server" Width="155px"></asp:Label>
            </ContentTemplate>
        </asp:UpdatePanel>
    </form>
</body>
</html>

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章