因为公司需要搞活动于是弄了一下倒计时得功能
<%@ Page Language="C#" AutoEventWireup="true" CodeFile="Default2.aspx.cs" Inherits="Default2" %>
<!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>
<asp:ScriptManager ID="ScriptManager1" runat="server" EnablePartialRendering="true">
</asp:ScriptManager>
<asp:UpdatePanel ID="UpdatePanel1" runat="server">
</asp:UpdatePanel>
<asp:Timer ID="Timer1" runat="server" Interval="1000" ontick="Timer1_Tick">
<asp:Label ID="Label2" runat="server" Text="Label"></asp:Label>
</asp:Timer>
<asp:Label ID="Label1" runat="server" Text="Label"></asp:Label>
</div>
</form>
</body>
</html>
代码文件
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Text;
public partial class Default2 : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
Label2.Text = "Page created at: " + DateTime.Now.ToLongTimeString();
}
protected void Timer1_Tick(object sender, EventArgs e)
{
ShowCountDown(GetOverTime());
}
/// <summary>
/// 得到结束得时间
/// </summary>
/// <returns></returns>
private DateTime GetOverTime()
{
DateTime overTime = ConnectionDB.getTimer("结束时间");
return overTime;
}
private void ShowCountDown(DateTime overTime)
{
try {
//当前时间
DateTime dtStart = System.DateTime.Now;
//获取两个时间得间隔
TimeSpan ts = new TimeSpan(0, 0, 90000);
ts = overTime.Subtract(dtStart);
string days = (ts.Days < 10) ? ("0" + ts.Days.ToString()) : ts.Days.ToString();
string hour = (ts.Hours < 10) ? ("0" + ts.Hours.ToString()) : ts.Hours.ToString();
string minutes = (ts.Minutes < 10) ? ("0" + ts.Minutes.ToString()) : ts.Minutes.ToString();
string seconds = (ts.Seconds < 10) ? ("0" + ts.Seconds.ToString()) : ts.Seconds.ToString();
int time_Hours = int.Parse((ts.Hours).ToString());
int time_Seconds = int.Parse((ts.Seconds).ToString());
// int time_Hours = Convert.ToInt32(ts.TotalHours);
int time_Minutes = int.Parse((minutes).ToString());
int time_days = int.Parse((ts.Days).ToString());
if (time_Minutes >= 60)
{
if ((time_Minutes % 60) != 0)
{
hour = (ts.Hours + 1).ToString();
time_Minutes -= 60;
}
}
if (time_Hours <= 0 && time_Minutes <= 0 && time_Seconds <= 0)
{
Label1.Text = "xxxxx活动正在进行中";
}
else
{
Label1.Text = "xxxxx活动还剩" + days + "天" + hour + "小时" + time_Minutes + "分钟" + seconds + "秒";
if (time_days <= 0)
{
Label1.Text = "xxxxx活动还剩" + hour + "小时" + time_Minutes + "分钟" + seconds + "秒";
if (time_Hours <= 0)
{
Label1.Text = "xxxxx活动还剩" + time_Minutes + "分钟" + seconds + "秒";
if (time_Minutes <= 0)
{
Label1.Text = "xxxxx活动还剩" + seconds + "秒";
}
}
}
}
}catch(Exception e){
Label1.Text = "活动已结束";
}
}
}
数据库连接
using System;
using System.Collections.Generic;
using System.Linq;
using System.Data.SqlClient;
using System.Data;
using System.Configuration;
using System.Web;
public class ConnectionDB
{
public static string connName = "ApplicationServices";
public static DateTime getTimer(string timeout)
{
SqlConnection conn = null;
try
{
string connStr = ConfigurationManager.ConnectionStrings[connName].ToString();
//初始化连接对象
conn = new SqlConnection();
conn.ConnectionString = connStr;
//打开连接
if (conn.State == ConnectionState.Closed)
{
conn.Open();
}
string sql = "select timer from pxp_time where text like '%" + timeout + "%'";
SqlCommand com = new SqlCommand(sql, conn);
SqlDataReader read = com.ExecuteReader();
while (read.Read())
{
DateTime dateTime = (DateTime)read["timer"];
return dateTime;
}
}
catch (Exception e)
{
Console.WriteLine(e.Message);
}
finally
{
if (conn != null)
{
//conn.Close();
}
}
return new DateTime();
}
}
web.config
<?xml version="1.0"?>
<!--
有关如何配置 ASP.NET 应用程序的详细信息,请访问
http://go.microsoft.com/fwlink/?LinkId=169433
-->
<configuration>
<connectionStrings>
<!--<add name="ApplicationServices"
connectionString="data source=.\SQLEXPRESS;Integrated Security=SSPI;AttachDBFilename=|DataDirectory|aspnetdb.mdf;User Instance=true"
providerName="System.Data.SqlClient" />-->
<!--<add name="ApplicationServices"
connectionString="data source=.\SQLEXPRESS;Initial Catalog=TestTimer;User ID=sa;Password=lsjk" providerName="System.Data.SqlClient" />-->
<add name="ApplicationServices" connectionString="Data Source=.;Initial Catalog=TestTimer;uid=sa;pwd=***;"/>
<!--<add name="Connection String" connectionString="server=139.129.251.206;uid=sa;pwd=px888..;database=longshengtouzi_ceshi" providerName="System.Data.SqlClient" />-->
</connectionStrings>
<system.web>
<compilation debug="true" targetFramework="4.0"/>
</system.web>
</configuration>
在附上在网上查到得js不过不能实现无刷新功能
<%@ Page Language="C#" AutoEventWireup="true" CodeFile="Default.aspx.cs" Inherits="_Default" %>
<!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>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<script src="script/jquery-3.2.1.js" />
<script type="text/javascript">
</head>
<script type="text/javascript">
var WillDoXmalHttp;
function CountDown() {
setInterval("WillDo()", 1000);
}
function WinllDo() {
createWillDoXmlHttpRequest();
var url = "Default.aspx";
WillDoXmlHttp.open("Get", url, true);
WillDoXmlHttp.onReadystatechange = WillDoCallback;
WillDoXmlHttp.send(null);
}
function createWillDoXmlHttpRequest() {
if (window.XMLHttpRequest) {
WillDoXmlHttp = new XMLHttpRequest();
} else if (window.ActiveXObject) {
try {
WillDoXmlHttp = new ActiveXObject("MsXml2.XmlHTTP");
} catch (e1) {
try {
WillDoXmlHttp = new ActiveXObject("Microsoft.XmlHTTP");
} catch (e2) { }
}
}
return WillDoXmlHttp;
}
function WillDoCallback() {
if (WillDoXmlHttp.readyState == 4) {
if (WillDoXmlHttp.status == 200) {
var dbsyContent = document.getElementById("divCountDown");
dbsyContent.innerHTML = WillDoXmlHttp.responseText;
}
}
window.status = "";
}
</script>
<body οnlοad="Countdown();">
<div id="divCountDown">
</div>
<form id="form1" runat="server">
<div>
</div>
</form>
</body>
</html>