本文實(shí)例講述了asp.net中Timer無(wú)刷新定時(shí)器的實(shí)現(xiàn)方法。Timer控件要實(shí)現(xiàn)無(wú)刷新,得用到ajax技術(shù),這里使用VS2008自帶的ajax技術(shù)。
首先得添加一個(gè)ScriptManager控件,然后再添加一個(gè)UpdatePanel用于存放Timer控件內(nèi)容的,就可以實(shí)現(xiàn)無(wú)刷新了。下面是詳細(xì)的內(nèi)容:
一、前臺(tái)代碼如下:
1
2
3
4
5
6
7
8
9
10
|
<form id= "form1" runat= "server" > <asp:ScriptManager ID= "ScriptManager1" runat= "server" > </asp:ScriptManager> <asp:UpdatePanel ID= "UpdatePanel1" runat= "server" > <ContentTemplate> <asp:Timer ID= "Timer1" runat= "server" Interval= "60000" ontick= "Timer1_Tick" > </asp:Timer> </ContentTemplate> </asp:UpdatePanel> </form> |
記得ScriptManager 一定要放在<form>標(biāo)簽內(nèi),可以放在任意地方。而添加UpdatePanel 控件后,要用到它一個(gè)很重要的屬性ContentTemplate,要不然就無(wú)法實(shí)現(xiàn)無(wú)刷新效果。在這里我們?cè)O(shè)置6秒定時(shí)觸發(fā)事件一次。
二、后臺(tái)代碼如下:
1
2
3
4
5
6
7
|
protected void Page_Load( object sender, EventArgs e) {} protected void Timer1_Tick( object sender, EventArgs e) { //這里可以操作你想做的事情,比如定時(shí)查詢數(shù)據(jù)庫(kù) ScriptManager.RegisterStartupScript( this , this .GetType(), "" , "alert('Hello‘);" , true ); } |
希望本文所述實(shí)例對(duì)大家asp.net程序設(shè)計(jì)有所幫助。