D:/Projects/Anthem/Anthem/Timer.cs

00001 using System;
00002 using System.ComponentModel;
00003 using System.Web.UI;
00004 using ASP = System.Web.UI.WebControls;
00005 
00006 namespace Anthem
00007 {
00011     public class Timer : Control //, IUpdatableControl
00012     {
00013         #region Declarations
00014 
00015         // Tracks whether the client timeout script should be added to the page.
00016         private bool _addTimeoutScript = false;
00017 
00018         #endregion
00019 
00020         #region Unique Anthem control code
00021 
00026         [DefaultValue(false)]
00027         public bool Enabled
00028         {
00029             get
00030             {
00031                 if (null == ViewState["Enabled"])
00032                     return false;
00033                 else
00034                     return (bool)ViewState["Enabled"];
00035             }
00036             set
00037             {
00038                 ViewState["Enabled"] = value;
00039             }
00040         }
00041 
00045         [DefaultValue(1000)]
00046         public int Interval
00047         {
00048             get
00049             {
00050                 if (null == ViewState["Interval"])
00051                     return 1000;
00052                 else
00053                     return (int)ViewState["Interval"];
00054             }
00055             set { ViewState["Interval"] = value; }
00056         }
00057 
00061         [Anthem.Method]
00062         public void DoTick()
00063         {
00064             OnTick(EventArgs.Empty);
00065         }
00066 
00070         public void StartTimer()
00071         {
00072             this.Enabled = true;
00073             _addTimeoutScript = true;
00074         }
00075 
00079         public void StopTimer()
00080         {
00081             this.Enabled = false;
00082             _addTimeoutScript = true;
00083         }
00084 
00088         public event EventHandler Tick;
00089 
00093         protected virtual void OnTick(EventArgs e)
00094         {
00095             if (Enabled && (Tick != null))
00096             {
00097                 _addTimeoutScript = true;
00098                 Tick(this, e);
00099             }
00100         }
00101 
00105         protected override void OnPreRender(EventArgs e)
00106         {
00107             base.OnPreRender(e);
00108 
00109             if (!Anthem.Manager.IsCallBack)
00110             {
00111                 // Always register the var that will track the timer.
00112                 string script = string.Format("<script type=\"text/javascript\">var {0}_timer = null;</script>", this.ClientID);
00113 #if V2
00114                 Page.ClientScript.RegisterStartupScript(this.GetType(), script, script, false);
00115 #else
00116                 Page.RegisterStartupScript(script, script);
00117 #endif
00118 
00119                 // For the initial page load, register the timer script so that it loads after
00120                 // the page is finished loading.
00121                 if (Enabled)
00122                 {
00123                     script = string.Format("<script type=\"text/javascript\">{0}</script>", GetSetTimeoutScript());
00124 #if V2
00125                     Page.ClientScript.RegisterStartupScript(this.GetType(), script, script, false);
00126 #else
00127                     Page.RegisterStartupScript(script, script);
00128 #endif
00129                 }
00130             }
00131             else if (_addTimeoutScript)
00132             {
00133                 // If this is a callback, add a script to call the timer again or to stop the timer.
00134                 if (Enabled)
00135                 {
00136                     Anthem.Manager.AddScriptForClientSideEval(GetSetTimeoutScript());
00137                 }
00138                 else
00139                 {
00140                     Anthem.Manager.AddScriptForClientSideEval(GetClearTimeoutScript());
00141                 }
00142 
00143             }
00144         }
00145         
00149         [Browsable(false)]
00150         public override bool Visible
00151         {
00152             get { return true; }
00153             set {}
00154         }
00155 
00156         #endregion
00157 
00158         #region Common Anthem control code
00159 
00160         // Note that the Timer control only needs to register. Since it is not
00161         // rendered in the traditional sense, the other common code is not needed.
00162 
00168         protected override void OnLoad(EventArgs e)
00169         {
00170             base.OnLoad(e);
00171             Anthem.Manager.Register(this);
00172         }
00173 
00174         #endregion
00175 
00176         #region Private Methods
00177 
00178         private string GetSetTimeoutScript()
00179         {
00180             return string.Format("{0} {1}_timer = setTimeout(function() {{ Anthem_InvokeControlMethod('{1}', 'DoTick', [], function() {{ }}, null) }}, {2})",
00181                 GetClearTimeoutScript(),
00182                 ClientID,
00183                 Interval
00184             );
00185         }
00186 
00187         private string GetClearTimeoutScript()
00188         {
00189             return string.Format("if (typeof({0}_timer) != 'undefined' && {0}_timer != null) {{ clearTimeout({0}_timer); {0}_timer = null; }}", ClientID);
00190         }
00191 
00192         #endregion
00193     }
00194 }

Generated on Wed Mar 14 23:50:39 2007 for Anthem by  doxygen 1.5.1-p1