Detailed descriptions of ASP. NET, AJAX, and Timer controls

Source: Internet
Author: User

Timer control solution:
Use the Timer control to perform the following operations:
Update the content of one or more UpdatePanel controls on a regular basis without refreshing the entire page.
The code on the server is run whenever the Timer control causes a return.
Publish the entire Web page to the Web server at a defined interval.

Background:
The Timer control is a server control that embeds a JavaScript component into a webpage. When the Interval is defined in the Interval attribute, the JavaScript component starts sending back from the browser. You can set the Timer control attributes in the code running on the server. These attributes are passed to the JavaScript component.
When using the Timer control, you must include the ScriptManager class instance on the webpage.
If the resend is started by the Timer control, the Timer control will trigger a Tick event on the server. When a page is sent to the server, you can create a Tick event handler to perform some operations.
Set the Interval attribute to specify the frequency of sending back, and set the Enabled attribute to enable or disable Timer. The Interval attribute is defined in milliseconds. The default value is 60,000 milliseconds (that is, 60 seconds ).

Note:
Setting the Interval attribute of the Timer control to a smaller value will generate a large amount of communication sent to the Web server. You can use the Timer control to refresh content only at the desired frequency.
If different UpdatePanel controls must be updated at different time intervals, You can include multiple Timer controls on the webpage. Alternatively, you can use a single instance of the Timer control as a trigger for multiple UpdatePanel controls on the webpage.
Use the Timer control inside the UpdatePanel Control
When the Timer control is included in the UpdatePanel control, the Timer control is automatically used as a trigger for the UpdatePanel control. You can override this behavior by setting the ChildrenAsTriggers attribute of the UpdatePanel control to false.
For the Timer control inside the UpdatePanel control, the JavaScript timing component is re-created only when each sending is complete. Therefore, the timing interval does not start until the page is returned from the resend. For example, if the Interval attribute is set to 60,000 milliseconds (60 seconds), but it takes 3 seconds to complete the sending back, the next sending back will take 63 seconds after the previous sending back.

The following example shows how to include the Timer control in the UpdatePanel control.

<Asp: ScriptManager runat = "server" id = "ScriptManager1"/>
<Asp: UpdatePanel runat = "server" id = "UpdatePanel1"
UpdateMode = "Conditional">
<Contenttemplate>
<Asp: Timer id = "Timer1" runat = "server"
Interval = "120000"
OnTick = "timereffectick">
</Asp: Timer>
</Contenttemplate>
</Asp: UpdatePanel>

Use the Timer control outside the UpdatePanel Control

When the Timer control is outside the UpdatePanel control, the Timer control must be explicitly defined as the trigger of the UpdatePanel control to be updated.

If the UpdatePanel control is outside the Timer control, the JavaScript timing component will continue to run during processing and sending. For example, if the Interval attribute is set to 60,000 milliseconds (60 seconds), and it takes 3 seconds to complete the re-sending, the next re-sending will take 60 seconds after the last re-sending. You can only view the refreshed content in the UpdatePanel Control for 57 seconds.

The Interval attribute must be set to the value that can be used to complete an asynchronous sending back before the next sending back is started. If a new sending back is started when an early sending back is processed, the first sending back will be canceled.
The following example shows how to use the Timer control outside the UpdatePanel control.

<Asp: ScriptManager runat = "server" id = "ScriptManager1"/>
<Asp: Timer ID = "Timer1" runat = "server" Interval = "120000"
OnTick = "timereffectick">
</Asp: Timer>
<Asp: UpdatePanel ID = "UpdatePanel1" runat = "server">
<Triggers>
<Asp: AsyncPostBackTrigger ControlID = "Timer1"
EventName = "Tick"/>
</Triggers>
<ContentTemplate>
<Asp: Label ID = "Label1" runat = "server"> </asp: Label>
</ContentTemplate>
</Asp: UpdatePanel>

Sample Code

The following example shows an UpdatePanel control that displays a randomly generated stock price and the generation time of the stock price. By default, the Timer control updates the content in UpdatePanel every 10 seconds. You can update the stock price every 10 seconds or 60 seconds, or do not update the stock price at all. When you choose not to update the stock price, the Enabled attribute is set to false.

<% @ Page Language = "VB" AutoEventWireup = "true" %>
 
<! DOCTYPE html PUBLIC "-// W3C // dtd xhtml 1.0 Transitional // EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
 
<Html xmlns = "http://www.w3.org/1999/xhtml">
<Head id = "Head1" runat = "server">
<Title> Timer Example Page </title>
<Script runat = "server">
Protected Sub Page_Load (ByVal sender As Object, ByVal e As EventArgs)
OriginalTime. Text = DateTime. Now. ToLongTimeString ()
End Sub
 
Protected Sub timereffectick (ByVal sender As Object, ByVal e As EventArgs)
StockPrice. Text = GetStockPrice ()
TimeOfPrice. Text = DateTime. Now. ToLongTimeString ()
End Sub
 
Private Function GetStockPrice () As String
Dim randomStockPrice As Double = 50 New Random (). NextDouble ()
Return randomStockPrice. ToString ("C ")
End Function
 
Protected Sub RadioButton1_CheckedChanged (ByVal sender As Object, ByVal e As System. EventArgs)
Timer1.integer = 10000
Timer1.Enabled = True
End Sub
 
Protected Sub RadioButton2_CheckedChanged (ByVal sender As Object, ByVal e As System. EventArgs)
Timer1.integer = 60000
Timer1.Enabled = True
End Sub
 
Protected Sub RadioButton3_CheckedChanged (ByVal sender As Object, ByVal e As System. EventArgs)
Timer1.Enabled = False
End Sub
</Script>
</Head>
<Body>
<Form id = "form1" runat = "server">
<Asp: ScriptManager ID = "ScriptManager1" runat = "server"/>
<Asp: Timer ID = "Timer1" OnTick = "timereffectick" runat = "server" Interval = "10000"/>
 
<Asp: UpdatePanel ID = "StockPricePanel" runat = "server" UpdateMode = "Conditional">
<Triggers>
<Asp: AsyncPostBackTrigger ControlID = "Timer1"/>
</Triggers>
<ContentTemplate>
Stock price is <asp: Label id = "StockPrice" runat = "server"> </asp: Label> <BR/>
As of <asp: Label id = "TimeOfPrice" runat = "server"> </asp: Label>
</ContentTemplate>
</Asp: UpdatePanel>
<Div>
<Asp: radioButton ID = "RadioButton1" AutoPostBack = "true" GroupName = "TimerFrequency" runat = "server" Text = "10 seconds" OnCheckedChanged = "RadioButton1_CheckedChanged"/> <br/>
<Asp: radioButton ID = "RadioButton2" AutoPostBack = "true" GroupName = "TimerFrequency" runat = "server" Text = "60 seconds" OnCheckedChanged = "RadioButton2_CheckedChanged"/> <br/>
<Asp: RadioButton ID = "RadioButton3" AutoPostBack = "true" GroupName = "TimerFrequency" runat = "server" Text = "Never" OnCheckedChanged = "RadioButton3_CheckedChanged"/> <br/
<Br/>
Page originally created at <asp: Label ID = "OriginalTime" runat = "server"> </asp: Label>
</Div>
</Form>
</Body>
</Html>

Related Article

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.