今天有人提问,写了一个。
回帖地址:http://topic.csdn.net/u/20081209/11/4598f196-3fb4-4513-b20d-7fd9da685012.html?seed=70049107
前台
- <%@ Page Language="C#" AutoEventWireup="true" CodeFile="tTimer.aspx.cs" Inherits="tTimer" %>
- <!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>定时器</title>
- <script type="text/javascript">
- //设置每五秒,从后台读取一次数据。
- document.onclick=function(){
- setInterval("gettxt()",5000);
- }
- function getById(id)
- {
- return document.getElementById(id);
- }
- //每隔五秒要执行的函数
- function gettxt()
- {
- var tb = getById("TextBox1");
- var v = tTimer.getRandom().value;//取得后台数据
- tb.value = v;
- return;
- }
- </script>
- </head>
- <body>
- <form id="form1" runat="server">
- <div>
- <asp:TextBox ID="TextBox1" runat="server"></asp:TextBox>
- </div>
- </form>
- </body>
- </html>
后台
- using System;
- using System.Data;
- using System.Configuration;
- using System.Collections;
- using System.Web;
- using System.Web.Security;
- using System.Web.UI;
- using System.Web.UI.WebControls;
- using System.Web.UI.WebControls.WebParts;
- using System.Web.UI.HtmlControls;
- public partial class tTimer : System.Web.UI.Page
- {
- protected void Page_Load(object sender, EventArgs e)
- {
- AjaxPro.Utility.RegisterTypeForAjax(typeof(tTimer));
- }
- [AjaxPro.AjaxMethod]
- public string getRandom()
- {
- Random random = new Random();
- return random.Next(100).ToString();
- }
- }