Timer组件。。抄的
Timer控件
1using System;
2using System.Web.UI;
3using System.ComponentModel;
4using System.Collections;
5using System.Text;
6
7namespace PowerAsp.NET.Controls
8{
9 /**//// <summary>
10 /// 书上抄的定时器例子。
11 /// </summary>
12 ///
13
14 public class WebTimer:Control,IPostBackEventHandler
15 {
16 public WebTimer()
17 {
18 }
19 private member#region private member
20 private int _interval = 5000;
21 private bool _enabled = false;
22 private static readonly object EventTimer = new object();
23 #endregion
24
25 property#region property
26 [Browsable(false)]
27 public bool Visible
28 {
29 get
30 {
31 return base.Visible;
32 }
33 set
34 {
35 base.Visible = value;
36 }
37 }
38
39 [ DefaultValue(5000),
40 Bindable(false),
41 Description("每次刷新的间隔")
42 ]
43 public int Interval
44 {
45 get
46 {
47 return _interval;
48 }
49 set
50 {
51 _interval = value;
52 }
53 }
54 [
55 DefaultValue(false),
56 Bindable(false),
57 Description("Timer是否可用")
58 ]
59 public bool Enabled
60 {
61 get
62 {
63 return _enabled;
64 }
65 set
66 {
67 _enabled = value;
68 }
69 }
70 #endregion
71
72 events#region events
73 public event EventHandler Timer
74 {
75 add
76 {
77 Events.AddHandler(EventTimer,value);
78 }
79 remove
80 {
81 Events.RemoveHandler(EventTimer,value);
82 }
83 }
84 #endregion
85
86 event handler functions#region event handler functions
87 protected virtual void OnTimer(EventArgs e)
88 {
89 EventHandler timerHandler = (EventHandler)Events[EventTimer];
90 if(timerHandler != null)
91 {
92 timerHandler(this,e);
93 }
94 }
95 #endregion
96
97 IPostBackEventHandler 成员#region IPostBackEventHandler 成员
98
99 void IPostBackEventHandler.RaisePostBackEvent(string eventArgument)
100 {
101 OnTimer(EventArgs.Empty);
102 }
103
104 #endregion
105
106 utility functions#region utility functions
107 private string buildJavaScript()
108 {
109 StringBuilder sb = new StringBuilder();
110 sb.Append("\n<script language=\"javascript\">\n<!--");
111 sb.AppendFormat("\n setTimeout(\"{0}\",{1});",new object[]{this.Page.GetPostBackEventReference(this),
112 _interval.ToString()
113 });
114 sb.Append("\n//-->\n</script>");
115 return sb.ToString();
116 }
117 #endregion
118
119 protected override void OnPreRender(EventArgs e)
120 {
121 base.OnPreRender (e);
122 Page.GetPostBackEventReference(this,"");
123 }
124
125
126 protected override void Render(HtmlTextWriter writer)
127 {
128 if(Page != null)
129 {
130 Page.VerifyRenderingInServerForm(this);
131 }
132 if(_enabled)
133 writer.Write(buildJavaScript());
134 }
135
136
137 }
138}
1using System;
2using System.Web.UI;
3using System.ComponentModel;
4using System.Collections;
5using System.Text;
6
7namespace PowerAsp.NET.Controls
8{
9 /**//// <summary>
10 /// 书上抄的定时器例子。
11 /// </summary>
12 ///
13
14 public class WebTimer:Control,IPostBackEventHandler
15 {
16 public WebTimer()
17 {
18 }
19 private member#region private member
20 private int _interval = 5000;
21 private bool _enabled = false;
22 private static readonly object EventTimer = new object();
23 #endregion
24
25 property#region property
26 [Browsable(false)]
27 public bool Visible
28 {
29 get
30 {
31 return base.Visible;
32 }
33 set
34 {
35 base.Visible = value;
36 }
37 }
38
39 [ DefaultValue(5000),
40 Bindable(false),
41 Description("每次刷新的间隔")
42 ]
43 public int Interval
44 {
45 get
46 {
47 return _interval;
48 }
49 set
50 {
51 _interval = value;
52 }
53 }
54 [
55 DefaultValue(false),
56 Bindable(false),
57 Description("Timer是否可用")
58 ]
59 public bool Enabled
60 {
61 get
62 {
63 return _enabled;
64 }
65 set
66 {
67 _enabled = value;
68 }
69 }
70 #endregion
71
72 events#region events
73 public event EventHandler Timer
74 {
75 add
76 {
77 Events.AddHandler(EventTimer,value);
78 }
79 remove
80 {
81 Events.RemoveHandler(EventTimer,value);
82 }
83 }
84 #endregion
85
86 event handler functions#region event handler functions
87 protected virtual void OnTimer(EventArgs e)
88 {
89 EventHandler timerHandler = (EventHandler)Events[EventTimer];
90 if(timerHandler != null)
91 {
92 timerHandler(this,e);
93 }
94 }
95 #endregion
96
97 IPostBackEventHandler 成员#region IPostBackEventHandler 成员
98
99 void IPostBackEventHandler.RaisePostBackEvent(string eventArgument)
100 {
101 OnTimer(EventArgs.Empty);
102 }
103
104 #endregion
105
106 utility functions#region utility functions
107 private string buildJavaScript()
108 {
109 StringBuilder sb = new StringBuilder();
110 sb.Append("\n<script language=\"javascript\">\n<!--");
111 sb.AppendFormat("\n setTimeout(\"{0}\",{1});",new object[]{this.Page.GetPostBackEventReference(this),
112 _interval.ToString()
113 });
114 sb.Append("\n//-->\n</script>");
115 return sb.ToString();
116 }
117 #endregion
118
119 protected override void OnPreRender(EventArgs e)
120 {
121 base.OnPreRender (e);
122 Page.GetPostBackEventReference(this,"");
123 }
124
125
126 protected override void Render(HtmlTextWriter writer)
127 {
128 if(Page != null)
129 {
130 Page.VerifyRenderingInServerForm(this);
131 }
132 if(_enabled)
133 writer.Write(buildJavaScript());
134 }
135
136
137 }
138}