asp.net中Timer定時器在web中無重新整理的使用

來源:互聯網
上載者:User

標籤:

最近在做一個項目的時候,web端的資料需要與資料來源進行即時同步,並保證資料的準確性,當時,考慮到使用ajax非同步重新整理技術。但後來在網上尋找相關資料時,發現這樣做,太浪費資源了,因為ajax的提交請求不應該這麼頻繁的,只適用於那種手動請求響應的那種,因此這種辦法是行不通了,後來,發現asp.net中有一個定時器Timer,可以進行即時同步資料,因此本人就做了一個小小的測試,發現還挺好用的,於是乎就有了下文。如下:

aspx頁面中的代碼:

<form id="form1" runat="server">     <div>    <asp:ScriptManager ID="ScriptManager1" runat="server"></asp:ScriptManager>      <asp:UpdatePanel ID="UpdatePanel1" runat="server">      <ContentTemplate>        <asp:Timer ID="Timer1" runat="server" Interval="1000" ontick="Timer1_Tick">        </asp:Timer>         update中的:<asp:Label  ID="Label2" runat="server" Text=""></asp:Label>      </ContentTemplate>           </asp:UpdatePanel>        當前秒:<asp:Label ID="Label1" runat="server" Text=""></asp:Label>    </div>   </form>

cs伺服器上的代碼:

 protected void Page_Load(object sender, EventArgs e)        {                   }        protected void Timer1_Tick(object sender, EventArgs e)        {            //這裡可以操作你想做的事情,比如定時查詢資料庫            Label1.Text = DateTime.Now.ToLongTimeString()+":"+DateTime.Now.Millisecond;            Label2.Text = DateTime.Now.ToLongTimeString() + ":" + DateTime.Now.Millisecond;        }

 

 說明一下:

要想使用Timer定時器進行無重新整理技術實現。首先在aspx頁面代碼中必須使用ScriptManager控制項,同時使用UpdatePanel控制項,這樣才會達到想要的結果。

測試後,你會發現,在UpdatePanel中的Label2對應的時間在不停的變化,和Lable1隻是在你第一次開啟頁面是 顯示,此後就不會顯示了。

 

asp.net中Timer定時器在web中無重新整理的使用

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在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.