<tr>
<td style="height:100px;">
<asp:UpdatePanel ID="UpdatePanel1" runat="server">
<ContentTemplate>当前时间是:
<!--Lable和Timer控件必须都包含在UpdatePanel控件中 -->
<asp:Label ID="Label1" runat="server" Text="Label"></asp:Label> <!--用于显示时间-->
<asp:Timer ID="Timer1" runat="server" Interval="1000"></asp:Timer><!-- 用于更新时间,每1秒更新一次-->
</ContentTemplate>
</asp:UpdatePanel>
</td>
</tr>
</table>
</form>
</body>
</html>
2、在ShowCurrentTime.aspx.cs文件中,只需要添加一句代码即可。代码如下:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
public partial class ShowCurrentTime : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
Label1.Text = DateTime.Now.ToString();
}
}
至此,完成了Label中实时显示时间的功能。另外,还可以根据需要设置时间显示的样式。
如果只想显示日期,而不显示时间,那么可以利用SubString取出前面的日期。
希望本文所述对大家的asp.net程序设计有所帮助。








