zl程序教程

您现在的位置是:首页 >  前端

当前栏目

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

JavaScriptNetASP 实现 时间 当前 不用 考虑
2023-06-13 09:14:13 时间
Default.aspx页面:先拉一个ScriptManager控件到页面,然后拉一个UpdatePanel控件。UpdatePanel里面放一个Label用于显示时间,放一个timer控件用于控制时间的更新。注意Label与Label都要放到UpdatePanel控件里面。最后,timer控件的Interval属性设置为1000,让它每1秒执行一次即更新时间。
Default.aspx.cs页面:只需在
protectedvoidPage_Load(objectsender,EventArgse)
里面输入
Label1.Text=DateTime.Now.ToString();
即可。
下面是两个页面的源码:
Default.aspx
复制代码代码如下:

<%@PageLanguage="C#"AutoEventWireup="true"CodeBehind="Default.aspx.cs"Inherits="动态显示实时时间._Default"%>
<!DOCTYPEhtmlPUBLIC"-//W3C//DTDXHTML1.0Transitional//EN""http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<htmlxmlns="http://www.w3.org/1999/xhtml">
<headrunat="server">
<title>动态显示实时时间</title>
<styletype="text/css">
.style1
{
height:183px;
}
</style>
</head>
<body>
<formid="form1"runat="server">
<div><asp:ScriptManagerID="ScriptManager1"runat="server">
</asp:ScriptManager>
</div>
<tablestyle="position:absolute;margin-left:200px;margin-right:200px;margin-top:100px;width:270px;height:78px;top:15px;left:10px;">
<tr>
<td>动态显示实时时间</td>
</tr>
<tr><tdclass="style1"><asp:UpdatePanelID="UpdatePanel1"runat="server">
<ContentTemplate>
当前时间是:
<asp:LabelID="Label1"runat="server"Text="Label"></asp:Label>
<asp:TimerID="Timer1"runat="server"Interval="1000">
</asp:Timer>
</ContentTemplate>
</asp:UpdatePanel>
</td></tr>
</table>
</form>
</body>
</html>

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

usingSystem;
usingSystem.Collections;
usingSystem.Configuration;
usingSystem.Data;
usingSystem.Linq;
usingSystem.Web;
usingSystem.Web.Security;
usingSystem.Web.UI;
usingSystem.Web.UI.HtmlControls;
usingSystem.Web.UI.WebControls;
usingSystem.Web.UI.WebControls.WebParts;
usingSystem.Xml.Linq;
namespace动态显示实时时间
{
publicpartialclass_Default:System.Web.UI.Page
{
protectedvoidPage_Load(objectsender,EventArgse)
{
Label1.Text=DateTime.Now.ToString();
}
}
}