- 浏览: 27072 次
- 性别:
- 来自: 成都
最新评论
这几天做的网站需要一个倒计时,如是作了一个如下的:
从上面HTML代码中可以看出,我使用了ASP.NET AJAX 中的Timer控件和JavaScript WebService两种方法来实现
ASP.NET AJAX Timer控件:
JavaScript WebService[CountdownService]:
运行效果:
<%@ Page Language="C#" AutoEventWireup="true" CodeFile="Countdown.aspx.cs" Inherits="Countdown" %> <!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>Countdown</title> <script type="text/javascript"> var totalSeconds;//剩余时间(秒) //倒计时函数 function startCountdown() { if(totalSeconds > 0) { var days = Math.floor(totalSeconds / 86400); var hours = Math.floor(totalSeconds % 86400 / 3600); var minutes = Math.floor(totalSeconds % 3600 / 60); var seconds = Math.floor(totalSeconds % 60); document.getElementById("lblDays").innerHTML = days; document.getElementById("lblHours").innerHTML = (hours >= 10 ? hours : "0" + hours); document.getElementById("lblMinutes").innerHTML = (minutes >= 10 ? minutes : "0" + minutes); document.getElementById("lblSeconds").innerHTML = (seconds >=10 ? seconds : "0" + seconds); totalSeconds--; } else { clearInterval(timer); } } function pageLoad(sender, args) { CountdownService.GetTotalSeconds(onSucceeded);//获取剩余时间(秒) } //成功获取剩余时间后的回调函数 function onSucceeded(result) { totalSeconds = result; var days = Math.floor(totalSeconds / 86400); var hours = Math.floor(totalSeconds % 86400 / 3600); var minutes = Math.floor(totalSeconds % 3600 / 60); var seconds = Math.floor(totalSeconds % 60); document.getElementById("lblDays").innerHTML = days; document.getElementById("lblHours").innerHTML = (hours >= 10 ? hours : "0" + hours); document.getElementById("lblMinutes").innerHTML = (minutes >= 10 ? minutes : "0" + minutes); document.getElementById("lblSeconds").innerHTML = (seconds >=10 ? seconds : "0" + seconds); } timer = setInterval("startCountdown()",1000);//开始倒计时 </script> </head> <body> <form id="form1" runat="server"> <asp:ScriptManager ID="ScriptManager1" runat="server"> <Services> <asp:ServiceReference Path="CountdownService.asmx" /> </Services> </asp:ScriptManager> <div style="border-style: solid;"> 使用Timer控件 <asp:UpdatePanel ID="UpdatePanelCountdown" runat="server" UpdateMode="Conditional"> <ContentTemplate> <asp:Label ID="lblTimerDays" runat="server" Text="0" ForeColor="Red" />天 <asp:Label ID="lblTimerHours" runat="server" Text="0" ForeColor="Red" />时 <asp:Label ID="lblTimerMinutes" runat="server" Text="0" ForeColor="Red" />分 <asp:Label ID="lblTimerSeconds" runat="server" Text="0" ForeColor="Red" />秒 <asp:Timer ID="Timer1" runat="server" Interval="1000" OnTick="Timer1_Tick1" /> </ContentTemplate> </asp:UpdatePanel> </div> <br /> <hr style="color: Lime;" /> <br /> <div id="DivCountdown" style="border-style: solid;"> 使用JavaScript<br /> <asp:Label ID="lblDays" runat="server" Text="0" ForeColor="Red" />天 <asp:Label ID="lblHours" runat="server" Text="0" ForeColor="Red" />时 <asp:Label ID="lblMinutes" runat="server" Text="0" ForeColor="Red" />分 <asp:Label ID="lblSeconds" runat="server" Text="0" ForeColor="Red" />秒 </div> </form> </body> </html>
从上面HTML代码中可以看出,我使用了ASP.NET AJAX 中的Timer控件和JavaScript WebService两种方法来实现
ASP.NET AJAX Timer控件:
using System; using System.Collections; using System.Configuration; using System.Data; using System.Linq; using System.Web; using System.Web.Security; using System.Web.UI; using System.Web.UI.HtmlControls; using System.Web.UI.WebControls; using System.Web.UI.WebControls.WebParts; using System.Xml.Linq; public partial class Countdown : System.Web.UI.Page { DateTime NowTime;//当前时间 DateTime EndTime = Convert.ToDateTime("2010-06-03 23:59:59");//结束时间 TimeSpan CountdownSpan;//时间间隔 protected void Page_Load(object sender, EventArgs e) { if (!IsPostBack) { NowTime = DateTime.Now; CountdownSpan = EndTime - NowTime; if (CountdownSpan.TotalSeconds > 0) { lblTimerDays.Text = CountdownSpan.Days.ToString(); lblTimerHours.Text = CountdownSpan.Hours > 10 ? CountdownSpan.Hours.ToString() : "0" + CountdownSpan.Hours.ToString(); lblTimerMinutes.Text = CountdownSpan.Minutes > 10 ? CountdownSpan.Minutes.ToString() : "0" + CountdownSpan.Minutes.ToString(); lblTimerSeconds.Text = CountdownSpan.Seconds > 10 ? CountdownSpan.Seconds.ToString() : "0" + CountdownSpan.Seconds.ToString(); } } } protected void Timer1_Tick1(object sender, EventArgs e) { NowTime = DateTime.Now; CountdownSpan = EndTime - NowTime; if (CountdownSpan.TotalSeconds > 0) { lblTimerDays.Text = CountdownSpan.Days.ToString(); lblTimerHours.Text = CountdownSpan.Hours > 10 ? CountdownSpan.Hours.ToString() : "0" + CountdownSpan.Hours.ToString(); lblTimerMinutes.Text = CountdownSpan.Minutes > 10 ? CountdownSpan.Minutes.ToString() : "0" + CountdownSpan.Minutes.ToString(); lblTimerSeconds.Text = CountdownSpan.Seconds > 10 ? CountdownSpan.Seconds.ToString() : "0" + CountdownSpan.Seconds.ToString(); } } }
JavaScript WebService[CountdownService]:
<%@ WebService Language="C#" Class="CountdownService" %> using System; using System.Web; using System.Web.Services; using System.Web.Services.Protocols; [WebService(Namespace = "http://tempuri.org/")] [WebServiceBinding(ConformsTo = WsiProfiles.BasicProfile1_1)] //若要允许使用 ASP.NET AJAX 从脚本中调用此 Web 服务,请取消对下行的注释。 [System.Web.Script.Services.ScriptService] public class CountdownService : System.Web.Services.WebService { [WebMethod] public int GetTotalSeconds() { DateTime NowTime = DateTime.Now; DateTime EndTime = Convert.ToDateTime("2010-06-03 23:59:59"); TimeSpan Countdown = EndTime - NowTime; return (Countdown.TotalSeconds > 0) ? (int)Countdown.TotalSeconds : 0; } }
运行效果:
发表评论
-
模仿邮箱添加附件
2011-03-02 11:43 0模仿邮箱添加附件 -
数据控件中使用OnClientClick出现服务器标记格式不正确
2010-09-11 16:45 1105数据控件中使用如下代码会出现解析错误(服务器标记格式不正确): ... -
Update Progress Indicator
2010-09-11 16:37 0This is quite needed and requir ... -
ASP.NET中弹出消息框
2009-09-14 13:47 1509最普通的写法是 Response.Write(" ... -
TreeView的节点联动
2009-09-14 13:08 1215最近在实习做的项目中,需要将所有省以及省下的市以树的形式展现出 ... -
Association references unmapped class的解决方法
2009-08-16 00:09 5736搞了半天,原来我的NHibernate.cfg.xml文件里没 ... -
错误:回发或回调参数无效
2009-08-10 16:09 1819错误信息: 回发或回调参数无效。在配置中使用 <pag ... -
C#连接数据库
2009-08-01 20:56 13221.C# 连 接Access 程序代码: using Sy ... -
BinaryCode
2009-08-01 17:22 652基础书上的一道题,也记下来吧。 给一个由整数组成的加密字符串, ... -
随机生成验证码
2009-07-31 19:49 0保存着吧,以后好用 & ... -
GridView中用CheckBox实现全选功能
2009-07-29 11:31 2625在.aspx的<title></title& ...
相关推荐
asp.net 倒计时器 倒计时代码asp.net 倒计时器 倒计时代码asp.net 倒计时器 倒计时代码
ASP.NET实现从后端获取时间,前段javascript无刷新倒计时。
倒计时功能,调用数据库适合新手,代码可以直接使用
该代码利用ASP.NET平台开发的一个js倒计时,功能已经设计成公用模块,在浏览器中不返回NAN的问题已经解决。
asp.net +sql+C#, 倒计时
该源码基于asp.net(C#),可以显示系统时间,同时也可以进行倒数计时,可以用于在线考试系统,来判断是不是到交卷时间。
ASP.NET里面的计时器(相当于控件) 很方便使用
全局计时器声明和调用首先引入命名空间 using System.Timers; using System.IO; 在 Global.asax 中加入静态对象(变量) public static Timer gtimer = null; // 定义全局定时器类对象 public static int gcount...
asp.net模仿淘宝聚划算一页多个倒计时例子和一个倒计时
本篇文章介绍了,asp.net中倒计时自动跳转页面的实现方法(使用javascript)。需要的朋友参考下
asp.net中倒计时自动跳转页面的实现方法(使用javascript).docx
4.1 ASP.NET开发人员的Ajax 86 4.2 改进原有ASP.NET网站 87 4.2.1 一个示例ASP.NET网站 88 4.2.2 配置现有的ASP.NET网站 88 4.3 ScriptManager: Ajax页面的大脑 90 4.3.1 理解ScriptManager 90 4.3.2 部署...
资源名:asp.net短信登录验证码和图片验证程序源码 资源类型:程序源代码 源码说明: 1.实现了验证码倒计时的功能 1.以下验证码例子采用的是先获取手机号文字验证码,如果文字验证码收不到,将采用语音播放验证码的...
jw_flv_player ASP.net中 播放FLV文件 并由时间控制等等 使用方法:如果你仅需要播放一个FLV文件[例如“video.flv”],那么只要把“video.flv”和压缩包内的flvplayer.swf复制到同一个目录内,然后在页面内添加
Asp.Net MVC C#倒数计时器完整源代码在“ /Home/Index.cshtml”中,当用户输入小时,分钟,秒,然后单击“开始”按钮时,输入将提交给Home控制器中的PostCountdownTimer操作。 在操作中,我们通过向DateTime.Now...
深入剖析ASP.NET组件设计]一书第三章关于ASP.NET运行原理讲述的补白 asp.net 运行机制初探(httpModule加载) 利用反射来查看对象中的私有变量 关于反射中创建类型实例的两种方法 ASP.Net应用程序的多进程模型 NET委托...
实现自定义时间 倒计时 各处都会用到的接收短信按钮 抽空封装了下 made by jxn
网站开发语言:ASP.NET C# 网站后台数据库:SQL Server 2005 技术特点: 该系统采用三层结构开发,核心层在App_code下 提示: 前台登陆地址:/MyWeb/Index.aspx 默认学生帐号/密码:2010114113/111 后台管理地址:/...
ASP.NET中Cookie对象的使用,利用Cookie对象防止重复投票代码,在代码中还有倒计时的代码,都是拿来就可以用的
短信验证button的倒计时