Use of the timer timer in the Web without flushing in ASP.

Source: Internet
Author: User

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.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

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.