当前位置: 首页 > 图文教程 > 网络编程 > ASP.NET > asp.net 实现动态显示当前时间(不用javascript不考虑开销)

ASP.NET
探讨:ASP.NET技术的学习顺序问题
关于ASP.NET在IIS一些问题的经验总结
ASP.NET中通过对话框方式下载文件
ASP.NET网络编程中常用到的27个函数集
ASP.NET生成静态网页的方法
ASP.NET 2.0中实现弹窗报警提示
复杂ASP.NET服务器控件调整小技巧
ASP.NET调用oracle存储过程实现快速分页
VB.NET实现窗体图标最小化到状态栏
ASP.NET技巧:DataGrid传统分页方式
ASP.NET里的事务处理
ASP.NET 2.0高级数据处理之数据绑定
ASP.NET多频道网站架构实现方法
.NET vs J2EE——面对SOA的荒谬与误解
ASP.NET 2.0中执行数据库操作命令之一
ASP.NET应用程序资源访问安全模型
ASP.NET 2.0中的Web和HTML服务器控件
对.NET Framework 反射的反思
带你走进ASP.NET(1)
带你走进ASP.NET(2)

ASP.NET 中的 asp.net 实现动态显示当前时间(不用javascript不考虑开销)


出处:互联网   整理: 软晨网(RuanChen.com)   发布: 2010-01-10   浏览: 68 ::
收藏到网摘: n/a

asp.net实现动态显示时间,无需用到javascrip,而是用了AJAX。 Default.aspx页面:先拉一个ScriptManager控件到页面,然后拉一个UpdatePanel控件。UpdatePanel里面放一个Label用于显示时间,放一个timer控件用于控制时间的更新。注意Label与Label都要放到UpdatePanel控件里面。最后,timer控件的Interval属性设置为1000,让它每1秒执行一次即更新时间。
Default.aspx.cs页面:只需在
protected void Page_Load(object sender, EventArgs e)
里面输入
Label1.Text = DateTime.Now.ToString();
即可。
下面是两个页面的源码:
Default.aspx
复制代码 代码如下:

<%@ Page Language="C#" AutoEventWireup="true" CodeBehind="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>
<style type="text/css">
.style1
{
height: 183px;
}
</style>
</head>
<body>
<form id="form1" runat="server">
<div> <asp:ScriptManager ID="ScriptManager1" runat="server">
</asp:ScriptManager>
</div>
<table style=" position: absolute; margin-left:200px; margin-right:200px; margin-top:100px; width:270px; height:78px; top: 15px; left: 10px;">
<tr>
<td>动态显示实时时间</td>
</tr>
<tr><td class="style1"><asp:UpdatePanel ID="UpdatePanel1" runat="server" >
<ContentTemplate>
当前时间是:
<asp:Label ID="Label1" runat="server" Text="Label"></asp:Label>
<asp:Timer ID="Timer1" runat="server" Interval="1000">
</asp:Timer>
</ContentTemplate>
</asp:UpdatePanel>
</td></tr>
</table>
</form>
</body>
</html>

Default.aspx.cs
复制代码 代码如下:

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;
namespace 动态显示实时时间
{
public partial class _Default : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
Label1.Text = DateTime.Now.ToString();
}
}
}