</>code
- <ext:PageManager ID="PageManager1" runat="server" />
- <ext:Timer ID="Timer1" Interval="3" Enabled="false" OnTick="Timer1_Tick" runat="server">
- </ext:Timer>
- <ext:Button ID="btnStartTimer" runat="server" Text="Start Timer" OnClick="btnStartTimer_Click">
- </ext:Button>
- <ext:Button ID="btnStopTimer" runat="server" Text="Stop Timer" OnClick="btnStopTimer_Click">
- </ext:Button>
- <br />
- <ext:Label ID="labServerTime" runat="server" Text="This is current datetime.">
- </ext:Label>
</>code
- protected void Timer1_Tick(object sender, EventArgs e)
- {
- labServerTime.Text = DateTime.Now.ToString();
- }
- protected void btnStartTimer_Click(object sender, EventArgs e)
- {
- Timer1.Enabled = true;
- }
- protected void btnStopTimer_Click(object sender, EventArgs e)
- {
- Timer1.Enabled = false;
- }
</>code
- [ToolboxData("<{0}:Timer Interval=\"30\" runat=\"server\"></{0}:Timer>")]
- [ToolboxBitmap(typeof(Timer), "res.toolbox_icons.Timer.bmp")]
- [Description("定时器")]
- [DefaultEvent("Tick")]
- public class Timer : ControlBase, IPostBackEventHandler
- {
- #region properties
- /// <summary>
- /// 是否可用
- /// </summary>
- [Category(CategoryName.OPTIONS)]
- [DefaultValue(true)]
- [Description("是否可用")]
- public virtual bool Enabled
- {
- get
- {
- object obj = ViewState["Enabled"];
- return obj == null ? true : (bool)obj;
- }
- set
- {
- ViewState["Enabled"] = value;
- }
- }
- /// <summary>
- /// 定时间隔(单位:秒)
- /// </summary>
- [Category(CategoryName.OPTIONS)]
- [DefaultValue(30)]
- [Description("定时间隔(单位:秒)")]
- public int Interval
- {
- get
- {
- object obj = ViewState["Interval"];
- return obj == null ? 30 : (int)obj;
- }
- set
- {
- ViewState["Interval"] = value;
- }
- }
- #endregion
- #region OnPreLoad
- protected override void OnPreLoad(object sender, EventArgs e)
- {
- base.OnPreLoad(sender, e);
- SaveAjaxProperty("Enabled", Enabled);
- }
- #endregion
- #region OnPreRender
- protected override void OnPreRender(EventArgs e)
- {
- base.OnPreRender(e);
- // 不渲染
- RenderWrapperDiv = false;
- string setupScript = String.Format("box.{0}=window.setInterval(function(){{{1}}}, {2});", ClientJavascriptID, GetPostBackEventReference(), Interval * 1000);
- if (Enabled)
- {
- AddPageFirstLoadAbsoluteScript(setupScript);
- }
- if (AjaxPropertyChanged("Enabled", Enabled))
- {
- string ajaxScript = String.Format("window.clearInterval(box.{0});", ClientJavascriptID);
- if (Enabled)
- {
- ajaxScript += setupScript;
- }
- AddAjaxPropertyChangedScript(ajaxScript);
- }
- }
- #endregion
- #region IPostBackEventHandler
- public void RaisePostBackEvent(string eventArgument)
- {
- OnTick(EventArgs.Empty);
- }
- #endregion
- #region OnTick
- private static readonly object _handlerKey = new object();
- /// <summary>
- /// 定时事件
- /// </summary>
- [Category(CategoryName.ACTION)]
- [Description("定时事件")]
- public event EventHandler Tick
- {
- add
- {
- Events.AddHandler(_handlerKey, value);
- }
- remove
- {
- Events.RemoveHandler(_handlerKey, value);
- }
- }
- protected virtual void OnTick(EventArgs e)
- {
- EventHandler handler = Events[_handlerKey] as EventHandler;
- if (handler != null)
- {
- handler(this, e);
- }
- }
- #endregion
- }
</>code
- protected override void OnPreRender(EventArgs e)
- {
- base.OnPreRender(e);
- // 不渲染
- RenderWrapperDiv = false;
- string setupScript = String.Format("box.{0}=window.setInterval(function(){{{1}}}, {2});", ClientJavascriptID, GetPostBackEventReference(), Interval * 1000);
- if (Enabled)
- {
- AddPageFirstLoadAbsoluteScript(setupScript);
- }
- }
</>code
- box.__0=window.setInterval(function(){__doPostBack('Timer1','');}, 3000);
如对本文有疑问,请提交到交流论坛,广大热心网友会为你解答!! 点击进入论坛