定時器簡單應用

來源:互聯網
上載者:User

今天有人提問,寫了一個。

回帖地址:http://topic.csdn.net/u/20081209/11/4598f196-3fb4-4513-b20d-7fd9da685012.html?seed=70049107

前台

  1. <%@ Page Language="C#" AutoEventWireup="true" CodeFile="tTimer.aspx.cs" Inherits="tTimer" %>
  2. <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
  3. <html xmlns="http://www.w3.org/1999/xhtml" >
  4. <head runat="server">
  5.     <title>定時器</title>
  6.     <script type="text/javascript">
  7.         //設定每五秒,從後台讀取一次資料。
  8.         document.onclick=function(){
  9.             setInterval("gettxt()",5000);
  10.         }
  11.         function getById(id) 
  12.         { 
  13.             return document.getElementById(id); 
  14.         } 
  15.         //每隔五秒要執行的函數
  16.         function gettxt()
  17.         { 
  18.             var tb = getById("TextBox1");
  19.             var v = tTimer.getRandom().value;//取得後台資料
  20.             tb.value = v;
  21.             return;
  22.         }
  23.     </script>
  24. </head>
  25. <body>
  26.     <form id="form1" runat="server">
  27.     <div>
  28.         <asp:TextBox ID="TextBox1" runat="server"></asp:TextBox>
  29.     </div>
  30.     </form>
  31. </body>
  32. </html>

後台

  1. using System;
  2. using System.Data;
  3. using System.Configuration;
  4. using System.Collections;
  5. using System.Web;
  6. using System.Web.Security;
  7. using System.Web.UI;
  8. using System.Web.UI.WebControls;
  9. using System.Web.UI.WebControls.WebParts;
  10. using System.Web.UI.HtmlControls;
  11. public partial class tTimer : System.Web.UI.Page
  12. {
  13.     protected void Page_Load(object sender, EventArgs e)
  14.     {
  15.         AjaxPro.Utility.RegisterTypeForAjax(typeof(tTimer));
  16.     }
  17.     [AjaxPro.AjaxMethod]
  18.     public string getRandom()
  19.     {
  20.         Random random = new Random();
  21.         return random.Next(100).ToString();
  22.     } 
  23. }

設定檔同上一篇。

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.