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

C#倒计时

2014年02月02日 ⁄ 综合 ⁄ 共 1518字 ⁄ 字号 评论关闭

private DateTime dtExam = DateTime.Parse("2010-07-26 17: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');
        }

 

前台代码:

<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>

抱歉!评论已关闭.