asp.net 實現(xiàn)動態(tài)顯示當前時間(不用javascript不考慮開銷)
更新時間:2009年11月03日 23:50:39 作者:
asp.net實現(xiàn)動態(tài)顯示時間,無需用到javascrip,而是用了AJAX。
Default.aspx頁面:先拉一個ScriptManager控件到頁面,然后拉一個UpdatePanel控件。UpdatePanel里面放一個Label用于顯示時間,放一個timer控件用于控制時間的更新。注意Label與Label都要放到UpdatePanel控件里面。最后,timer控件的Interval屬性設置為1000,讓它每1秒執(zhí)行一次即更新時間。
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="動態(tài)顯示實時時間._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>動態(tài)顯示實時時間</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>動態(tài)顯示實時時間</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 動態(tài)顯示實時時間
{
public partial class _Default : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
Label1.Text = DateTime.Now.ToString();
}
}
}
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="動態(tài)顯示實時時間._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>動態(tài)顯示實時時間</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>動態(tài)顯示實時時間</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 動態(tài)顯示實時時間
{
public partial class _Default : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
Label1.Text = DateTime.Now.ToString();
}
}
}
相關文章
ASP.NET Gridview 中使用checkbox刪除的2種方法實例分享
ASP.NET Gridview 中使用checkbox刪除的2種方法實例分享,需要的朋友可以參考一下2013-06-06ASP.NET Core Zero使用Power Tool工具
這篇文章介紹了ASP.NET Core Zero使用Power Tool工具的方法,對大家的學習或者工作具有一定的參考學習價值,需要的朋友們下面隨著小編來一起學習學習吧2022-02-02asp.net為網(wǎng)頁動態(tài)添加關鍵詞的方法
這篇文章主要介紹了asp.net為網(wǎng)頁動態(tài)添加關鍵詞的方法,可實現(xiàn)動態(tài)添加keyword meta的功能,非常具有實用價值,需要的朋友可以參考下2015-04-04ASP.NET筆記之 Request 、Response 與Server的使用
本篇文章小編為大家介紹,ASP.NET筆記之 Request 、Response 與Server的使用。需要的朋友參考下2013-04-04ASP.NET?Core項目中調用WebService的方法
這篇文章介紹了ASP.NET?Core項目中調用WebService的方法,文中通過示例代碼介紹的非常詳細。對大家的學習或工作具有一定的參考借鑒價值,需要的朋友可以參考下2022-03-03