1. WEB服务器控件继承于System.Web.UI.Control类和System.Web.UI.IPostBackDataHandler 接口
2. 定义属性Text ,在属性中使用ViewState保存状态.声明public event EventHandler TextChanged;
3.实现 System.Web.UI.IPostBackDataHandler 接口的两个方法:
public bool LoadPostData(string postDataKey,
NameValueCollection postCollection);
public void RaisePostDataChangedEvent();
}源码如下:
1
using System;
2
using System.Web.UI;
3
using System.Web.UI.WebControls;
4
using System.ComponentModel;
5
using System.Text;
6
using System.Collections.Specialized;
7
8
namespace SamWebControlLib
9
{
10
/// <summary>
11
/// 输出一个意见输入框
12
/// </summary>
13
[DefaultProperty("Text"),
14
ToolboxData("<{0}:AuditingIdea runat=server></{0}:AuditingIdea>")]
15
public class AuditingIdea : System.Web.UI.Control,IPostBackDataHandler
16
{
17
private System.Web.UI.WebControls.TextBox tb;
18
19
private string _Title;
20
/// <summary>
21
/// 标题
22
/// </summary>
23
[Bindable(true),
24
Category("标题"),
25
DefaultValue("")]
26
public string Title
27
{
28
set
29
{
30
this._Title = value;
31
}
32
get
33
{
34
return this._Title;
35
}
36
}
37
/// <summary>
38
/// 文本框的值
39
/// </summary>
40
[Bindable(true),
41
Category("文本框的值"),
42
DefaultValue("")]
43
public String Text
44
{
45
get
46
{
47
return (String) ViewState["Text"];
48
}
49
set
50
{
51
ViewState["Text"] = value;
52
}
53
}
54
55
56
private string _TextBoxID;
57
/// <summary>
58
/// 文本框的值
59
/// </summary>
60
[Bindable(true),
61
Category("文本框设置"),
62
DefaultValue("")]
63
public string TextBoxID
64
{
65
set
66
{
67
this._TextBoxID = value;
68
}
69
get
70
{
71
return this._TextBoxID;
72
}
73
}
74
public event EventHandler TextChanged;
75
76
77
protected override void OnInit(EventArgs e)
78
{
79
InitializeComponent();
80
base.OnInit (e);
81
}
82
83
private void InitializeComponent()
84
{
85
this.tb = new TextBox();
86
this.tb.ID = this._TextBoxID;
87
this.tb.Width = 500;
88
this.tb.Height = 95;
89
this.tb.TextMode = TextBoxMode.MultiLine;
90
}
91
/// <summary>
92
/// 将此控件呈现给指定的输出参数。
93
/// </summary>
94
/// <param name="output"> 要写出到的 HTML 编写器 </param>
95
protected override void Render(HtmlTextWriter output)
96
{
97
output.WriteLine("<table width='100%' border=0 cellpadding=0 cellspacing=0>");
98
output.WriteLine("<TR>");
99
output.Write("<TD width='14%' valign='top'> <span class='zclc-txt1'><IMG border='0' src='../images/edit_ico.gif'>");
100
output.WriteLine(this._Title);
101
output.WriteLine("</span></TD>");
102
output.WriteLine("<TD width='86%'>");
103
this.tb.RenderControl(output);
104
output.WriteLine("</TD></TR></table>");
105
}
106
/// <summary>
107
/// 装载回发数据
108
/// </summary>
109
/// <param name="postDataKey"></param>
110
/// <param name="values"></param>
111
/// <returns></returns>
112
public virtual bool LoadPostData(string postDataKey, NameValueCollection values)
113
{
114
String presentValue;
115
if(Text!=null)
116
presentValue = Text;
117
else
118
presentValue = "";
119
String postedValue = values[postDataKey];
120
if (!presentValue.Equals(postedValue))
121
{
122
Text = postedValue;
123
return true;
124
}
125
return false;
126
}
127
128
public virtual void RaisePostDataChangedEvent()
129
{
130
OnTextChanged(EventArgs.Empty);
131
}
132
133
protected virtual void OnTextChanged(EventArgs e)
134
{
135
if (TextChanged != null)
136
TextChanged(this,e);
137
}
138
139
}
140
}
141
142
using System;2
using System.Web.UI;3
using System.Web.UI.WebControls;4
using System.ComponentModel;5
using System.Text;6
using System.Collections.Specialized;7

8
namespace SamWebControlLib9
{10
/// <summary>11
/// 输出一个意见输入框12
/// </summary>13
[DefaultProperty("Text"),14
ToolboxData("<{0}:AuditingIdea runat=server></{0}:AuditingIdea>")]15
public class AuditingIdea : System.Web.UI.Control,IPostBackDataHandler 16
{17
private System.Web.UI.WebControls.TextBox tb;18

19
private string _Title;20
/// <summary>21
/// 标题22
/// </summary>23
[Bindable(true),24
Category("标题"),25
DefaultValue("")]26
public string Title27
{28
set29
{30
this._Title = value;31
}32
get33
{34
return this._Title;35
}36
}37
/// <summary>38
/// 文本框的值39
/// </summary>40
[Bindable(true),41
Category("文本框的值"),42
DefaultValue("")]43
public String Text 44
{45
get 46
{47
return (String) ViewState["Text"];48
}49
set 50
{ 51
ViewState["Text"] = value;52
} 53
}54

55

56
private string _TextBoxID;57
/// <summary>58
/// 文本框的值59
/// </summary>60
[Bindable(true),61
Category("文本框设置"),62
DefaultValue("")]63
public string TextBoxID64
{65
set66
{67
this._TextBoxID = value;68
}69
get70
{71
return this._TextBoxID;72
}73
} 74
public event EventHandler TextChanged;75

76

77
protected override void OnInit(EventArgs e)78
{79
InitializeComponent();80
base.OnInit (e);81
}82

83
private void InitializeComponent() 84
{ 85
this.tb = new TextBox();86
this.tb.ID = this._TextBoxID;87
this.tb.Width = 500;88
this.tb.Height = 95;89
this.tb.TextMode = TextBoxMode.MultiLine;90
}91
/// <summary>92
/// 将此控件呈现给指定的输出参数。93
/// </summary>94
/// <param name="output"> 要写出到的 HTML 编写器 </param>95
protected override void Render(HtmlTextWriter output)96
{97
output.WriteLine("<table width='100%' border=0 cellpadding=0 cellspacing=0>");98
output.WriteLine("<TR>");99
output.Write("<TD width='14%' valign='top'> <span class='zclc-txt1'><IMG border='0' src='../images/edit_ico.gif'>");100
output.WriteLine(this._Title);101
output.WriteLine("</span></TD>");102
output.WriteLine("<TD width='86%'>");103
this.tb.RenderControl(output);104
output.WriteLine("</TD></TR></table>");105
}106
/// <summary>107
/// 装载回发数据108
/// </summary>109
/// <param name="postDataKey"></param>110
/// <param name="values"></param>111
/// <returns></returns>112
public virtual bool LoadPostData(string postDataKey, NameValueCollection values) 113
{114
String presentValue;115
if(Text!=null)116
presentValue = Text;117
else118
presentValue = "";119
String postedValue = values[postDataKey];120
if (!presentValue.Equals(postedValue))121
{122
Text = postedValue;123
return true;124
}125
return false;126
}127

128
public virtual void RaisePostDataChangedEvent() 129
{130
OnTextChanged(EventArgs.Empty); 131
}132
133
protected virtual void OnTextChanged(EventArgs e)134
{135
if (TextChanged != null)136
TextChanged(this,e);137
}138

139
}140
}141

142


