现在的位置: 首页 > 综合 > 正文

c#时间倒计时

2013年10月13日 ⁄ 综合 ⁄ 共 2198字 ⁄ 字号 评论关闭

后台代码:
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>

抱歉!评论已关闭.