Automatic Updates with the Timer


The question that immediately follows when someone sees what that they can easily update parts of the page asynchronously using a trigger is “How can I make it update automatically?” The Timer control makes this easy! Instead of requiring the user to perform an action in order to trigger updates, the Timer control will initiate the postback automatically. Listing 2-6 (Timer.aspx) shows the primary controlling property; the interval is set to 1000 milliseconds. The Timer will initiate a postback every second.

Listing 2-6

image from book
 <!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  runat="server"> <script language="C#" runat="server">     protected override void  OnLoad(EventArgs e) {         base.OnLoad(e);         System.Threading.Thread.Sleep(100);         string theTime = DateTime.Now.ToLongTimeString();         for(int i = 0; i < 3; i++) {             theTime += "<br />" + theTime;         }         time1.Text = theTime;         time2.Text = theTime;     } </script>     <title>Timer</title> </head> <body> <form  runat="server"> <div> <asp:ScriptManager  runat="server"></asp:ScriptManager> <asp:UpdatePanel  runat="server" UpdateMode="Always">     <ContentTemplate>         <div style="border-style:solid;background-color:gray;">         <asp:Label runat="server" ></asp:Label><br />                 </div><br />                 <asp:Timer  runat="server" Interval="1000"   >         </asp:Timer>     </ContentTemplate> </asp:UpdatePanel><br /> </div> </form> </body> </html>
image from book

The Timer can be placed directly inside the UpdatePanel to act as a child control trigger. It can also be placed anywhere and configured as the AsyncPostBackTrigger explicitly. A single timer can be the trigger for multiple UpdatePanels.

Listing 2-7 (SingleTimer.aspx) shows a modification of a previous example that has two UpdatePanels, and this time we’ll add a Timer. The first UpdatePanel has Always for the UpdateMode, so the Timer inside it will make it update on every Tick event. The second UpdatePanel set the UpdateMode to conditional and therefore requires that a trigger be defined. So it is wired up to the Timer from the first UpdatePanel.

Listing 2-7

image from book
 <!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  runat="server"> <script language="C#" runat="server">     protected override void  OnLoad(EventArgs e) {         base.OnLoad(e);         System.Threading.Thread.Sleep(100);         string theTime = DateTime.Now.ToLongTimeString();         for(int i = 0; i < 3; i++) {             theTime += "<br />" + theTime;         }         time1.Text = theTime;         time2.Text = theTime;     } </script>     <title>Timer</title> </head> <body> <form  runat="server"> <div> <asp:ScriptManager  runat="server"></asp:ScriptManager> <asp:UpdatePanel  runat="server" UpdateMode="Always"> <ContentTemplate> <asp:Timer  runat="server" Interval="1000"   ></asp:Timer>     <div style="border-style:solid;background-color:gray;">     <asp:Label runat="server" ></asp:Label><br />             </div><br /> </ContentTemplate> </asp:UpdatePanel><br /> <asp:UpdatePanel  runat="server"> <Triggers> <asp:AsyncPostBackTrigger Control EventName="Tick" /> </Triggers> <ContentTemplate>     <div style="border-style:solid;background-color:gray;">     <asp:Label runat="server" ></asp:Label><br />     </div><br /> </ContentTemplate> </asp:UpdatePanel> </div> </form> </body> </html>
image from book

And, of course, you can also place the Timer outside the UpdatePanel to trigger a full postback at regular intervals. I picked an interval of one second to demonstrate that a one-second interval does not mean the refresh will occur every second. The Timer instructs the browser to trigger an event after one second. This accuracy is determined by the granularity of the specific browser, so there will normally be a small additional delay. Once the Timer is signaled by the browser, it can raise its Tick event and the refresh will occur. You can see that the combination of different interactions will result in the refresh occurring at approximately one second, but it is not exact.




Professional ASP. NET 2.0 AJAX
Professional ASP.NET 2.0 AJAX (Programmer to Programmer)
ISBN: 0470109629
EAN: 2147483647
Year: 2004
Pages: 107

flylib.com © 2008-2017.
If you may any questions please contact us: flylib@qtcs.net