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