Ajax无刷新实现定时更新数据
1、引用 ajax.dll 实现走动时间显示来查看效果
前台代码
<script language="javascript" type="text/javascript">
//定时更新时间
function refresh()
{
var div = document.all("divTime");
var strTime = Default1.GetAllOnLineUser().value;
if (strTime != "")
div.innerHTML = strTime;
//1秒钟刷新
window.setTimeout(refresh, 1000);
}
</script>
<body>
<form id="form1" runat="server">
<div id="divTime"></div>
<script>
refresh();
</script>
</form>
</body>
后台代码:
protected void Page_Load(object sender, EventArgs e)
{
//注册Ajax类型
Ajax.Utility.RegisterTypeForAjax(typeof(Default1));
}
[Ajax.AjaxMethod()]
public string GetAllOnLineUser()
{
return "当前时间:" + DateTime.Now.ToString();
}
2、使用 Ajax 控件实现定时刷新数据
HTML代码:
<body>
<form id="form1" runat="server">
<div>
<asp:ScriptManager ID="ScriptManager1" runat="server">
</asp:ScriptManager>
<asp:UpdatePanel ID="UpdatePanel1" runat="server" ChildrenAsTriggers="true">
<ContentTemplate>
<asp:Timer ID="Timer1" runat="server" OnTick="Timer1_Tick" Interval="1000">
</asp:Timer>
<asp:GridView ID="GridView1" runat="server">
</asp:GridView>
</ContentTemplate>
</asp:UpdatePanel>
</div>
</form>
</body>
后台代码:
protected void Page_Load(object sender, EventArgs e)
&n