Asp.net ajax定时刷新页面
2011-03-05 11:26 追忆似水流年 阅读(1584) 评论(0) 编辑 收藏 举报
要实现定时的刷新页面 使用Timer控件,<asp:Timer Interval="5000" runat="server" />
Time控件需要配置触发器,触发器事件被触发,UpdatePanel就会刷新页面。触发器需要配置的属性有 ControlID 和 EventName。以下是具体的例子。
<%@ Page Language="C#" %>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
"http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<script runat="server">
protected void Page_Load(object sender, EventArgs e){
CurrentTime.Text = DateTime.Now.ToLongTimeString( );
}
</script>
<html xmlns="http://www.w3.org/1999/xhtml">
<head id="Head1" runat="server">
<title>ASP.NET AJAX</title>
</head>
<body>
<form id="form1" runat="server">
<asp:ScriptManager ID="ScriptManager1" runat="server">
</asp:ScriptManager>
<asp:Timer ID="FiveSeconds" Interval="5000" runat="server" />
<asp:UpdatePanel ID="UpdatePanel1" runat="server">
<ContentTemplate>
<asp:Label ID="CurrentTime" runat="server" />
</ContentTemplate>
<Triggers>
<asp:AsyncPostBackTrigger ControlID="FiveSeconds" EventName="Tick" />
</Triggers>
</asp:UpdatePanel>
</form>
</body>
</html>
Time控件需要配置触发器,触发器事件被触发,UpdatePanel就会刷新页面。触发器需要配置的属性有 ControlID 和 EventName。以下是具体的例子。
<%@ Page Language="C#" %>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
"http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<script runat="server">
protected void Page_Load(object sender, EventArgs e){
CurrentTime.Text = DateTime.Now.ToLongTimeString( );
}
</script>
<html xmlns="http://www.w3.org/1999/xhtml">
<head id="Head1" runat="server">
<title>ASP.NET AJAX</title>
</head>
<body>
<form id="form1" runat="server">
<asp:ScriptManager ID="ScriptManager1" runat="server">
</asp:ScriptManager>
<asp:Timer ID="FiveSeconds" Interval="5000" runat="server" />
<asp:UpdatePanel ID="UpdatePanel1" runat="server">
<ContentTemplate>
<asp:Label ID="CurrentTime" runat="server" />
</ContentTemplate>
<Triggers>
<asp:AsyncPostBackTrigger ControlID="FiveSeconds" EventName="Tick" />
</Triggers>
</asp:UpdatePanel>
</form>
</body>
</html>