In a recent project, web-side data needed to be synchronized with the data source in real-time, and to ensure the accuracy of the data, at that time, taking into account the use of Ajax asynchronous refresh technology. But later on the internet to find the relevant information, found that this is a waste of resources, because the AJAX submission request should not be so frequent, only applicable to the kind of manual request response, so this method is not feasible, and later, Found that there is a timer in ASP. Timer, can be real-time synchronization of data, so I did a small test, found that it is very useful, and then have the following. As follows:
The code in the ASPX page:
<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=" +" Ontick="Timer1_Tick"> </asp:Timer>in the update:<asp:label id="Label2"runat="Server"text=""></asp:Label> </ContentTemplate> </asp:UpdatePanel>Current seconds:<asp:label id="Label1"runat="Server"text=""></asp:Label> </div> </form>
The code on the CS server:
protected voidPage_Load (Objectsender, EventArgs e) { } protected voidTimer1_Tick (Objectsender, EventArgs e) { //Here you can do what you want to do, such as querying the database regularlyLabel1.Text = DateTime.Now.ToLongTimeString () +":"+DateTime.Now.Millisecond; Label2.Text= DateTime.Now.ToLongTimeString () +":"+DateTime.Now.Millisecond; }
explain:
To use the timer timer for no-refresh technology implementation. You must first use the ScriptManager control in the ASPX page code and use the UpdatePanel control to achieve the desired result.
After testing, you will find that the Label2 corresponding time in the UpdatePanel is constantly changing, and Lable1 just when you first open the page is displayed and thereafter will not be displayed.
Use of the timer timer in the Web without flushing in ASP.