<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="/1999/xhtml" >
c#时间倒计时
后台代码:
using System;
using System.Data;
using System.Configuration;
using System.Collections;
using System.Web;
using System.Web.Security;
using System.Web.UI;
</ContentTemplate>
</asp:UpdatePanel>
</form>
</body>
</html>
Hale Waihona Puke <head runat="server">
<title>倒计时</title>
</head>
<body>
<form id="form1" runat="server">
<div>
</div>
<asp:TextBox ID="TextBox3" runat="server"></asp:TextBox>
</asp:Timer>
<asp:Label ID="labDays" runat="server" Width="155px"></asp:Label><asp:Label ID="labTimes" runat="server" Width="155px"></asp:Label>
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) //如果设置的时间已过
bDays.Text = ts.Days.ToString().PadLeft(2, '0') + "天";
bTimes.Text = ts.Hours.ToString().PadLeft(2, '0') + ":" + ts.Minutes.ToString().PadLeft(2, '0') + ":" + ts.Seconds.ToString().PadLeft(2, '0');
</asp:ScriptManager>
<asp:UpdatePanel ID="UpdatePanel1" runat="server">
<ContentTemplate>
<asp:Timer ID="timerCD" runat="server" Interval="1000" OnTick="timerCD_Tick">
{
this.timerCD.Enabled = false; //将Timmer置为false
labTimes.Text = "时间到!";
}
else
}
}
}
前台代码:
<%@ Page Language="C#" AutoEventWireup="true" CodeBehind="Timer.aspx.cs" Inherits="AJAXEnabledWebApplication1.Timer" %>
{
RefreshTime(); //刷新时间
}
}
private void RefreshTime() //刷新时间的方法
{
TimeSpan ts = this.dtExam - DateTime.Now; //时间差
<asp:TextBox ID="TextBox1" runat="server"></asp:TextBox>
时<asp:TextBox ID="TextBox2" runat="server"></asp:TextBox>分<asp:ScriptManager ID="ScriptManager1" runat="server">