一个简单的文本框控件
这一个简单的文本框控件主要实现在了IPostBackDataHandler, IEditableTextControl, ItextControl三个接口。IPostBackDataHandler
IEditableTextControl主要有事件event EventHandler TextChanged;
有ItextControl主要有一个Text属性
接口说明:
IPostBackDataHandler 主要有bool LoadPostData(string postDataKey, NameValueCollection postCollection);void RaisePostDataChangedEvent();二个方法。
用法:用于有数据回传时。在本例子中通过
1
PostBackOptions options1 = new PostBackOptions(this, string.Empty);
2
if (this.Page.Form != null)
3
{
4
options1.AutoPostBack = true;
5
text4 = Util.MergeScript(text4, page1.ClientScript.GetPostBackEventReference(options1, true));
6
writer.AddAttribute(HtmlTextWriterAttribute.Onchange, text4);
7
writer.AddAttribute("language", "javascript", false);
8
}
PostBackOptions options1 = new PostBackOptions(this, string.Empty);2
if (this.Page.Form != null)3
{4
options1.AutoPostBack = true;5
text4 = Util.MergeScript(text4, page1.ClientScript.GetPostBackEventReference(options1, true));6
writer.AddAttribute(HtmlTextWriterAttribute.Onchange, text4);7
writer.AddAttribute("language", "javascript", false);8
}
从而实现在InventTextBox的AutoPostBack属性为True时回传数据。从而调用bool LoadPostData(string postDataKey, NameValueCollection postCollection)
在上方法返回为真时再调用void RaisePostDataChangedEvent();
2、IEditableTextControl有事件event EventHandler TextChanged;
3、ItextControl有一个Text属性
具本例子如下:
1
using System;
2
using System.Collections.Generic;
3
using System.Text;
4
using System.Web;
5
using System.Drawing.Design;
6
using System.Globalization;
7
using System.Web.UI;
8
using System.Web.UI.WebControls;
9
using System.Security.Permissions;
10
using System.Collections.Specialized;
11
using System.ComponentModel;
12
[assembly: TagPrefix("Invent.Controls", "Invent")]
13
namespace InventControls
14
{
15
[ToolboxData("<{0}:InventTextBox runat=server></{0}:InventTextBox>")]
16
public class InventTextBox : WebControl, IPostBackDataHandler, IEditableTextControl, ITextControl
17
{
18
public event EventHandler TextChanged
19
{
20
add
21
{
22
base.Events.AddHandler(InventTextBox.EventTextChanged, value);
23
}
24
remove
25
{
26
base.Events.RemoveHandler(InventTextBox.EventTextChanged, value);
27
}
28
}
29
30
static InventTextBox()
31
{
32
InventTextBox.EventTextChanged = new object();
33
}
34
35
public InventTextBox() : base(HtmlTextWriterTag.Input)
36
{
37
}
38
protected override void AddAttributesToRender(HtmlTextWriter writer)
39
{
40
Page page1 = this.Page;
41
if (page1 != null)
42
{
43
page1.VerifyRenderingInServerForm(this);
44
}
45
string text1 = this.UniqueID;
46
if (text1 != null)
47
{
48
writer.AddAttribute(HtmlTextWriterAttribute.Name, text1);
49
}
50
string text3 = this.Text;
51
if (text3.Length > 0)
52
{
53
writer.AddAttribute(HtmlTextWriterAttribute.Value, text3);
54
}
55
if (this.ReadOnly)
56
{
57
writer.AddAttribute(HtmlTextWriterAttribute.ReadOnly, "readonly");
58
}
59
60
if (this.AutoPostBack && this.Page != null)
61
{
62
string text4 = null;
63
if (base.HasAttributes)
64
{
65
text4 = base.Attributes["onchange"];
66
if (text4 != null)
67
{
68
text4 = Util.EnsureEndWithSemiColon(text4);
69
base.Attributes.Remove("onchange");
70
}
71
}
72
PostBackOptions options1 = new PostBackOptions(this, string.Empty);
73
if (this.Page.Form != null)
74
{
75
options1.AutoPostBack = true;
76
text4 = Util.MergeScript(text4, page1.ClientScript.GetPostBackEventReference(options1, true));
77
writer.AddAttribute(HtmlTextWriterAttribute.Onchange, text4);
78
writer.AddAttribute("language", "javascript", false);
79
}
80
}
81
else if (page1 != null)
82
{
83
page1.ClientScript.RegisterForEventValidation(this.UniqueID, string.Empty);
84
}
85
if (this.Enabled && !base.IsEnabled)
86
{
87
writer.AddAttribute(HtmlTextWriterAttribute.Disabled, "disabled");
88
}
89
base.AddAttributesToRender(writer);
90
}
91
protected override void OnPreRender(EventArgs e)
92
{
93
base.OnPreRender(e);
94
}
95
protected override void Render(HtmlTextWriter writer)
96
{
97
this.RenderBeginTag(writer);
98
if (this.TextMode == TextBoxMode.MultiLine)
99
{
100
HttpUtility.HtmlEncode(this.Text, writer);
101
}
102
this.RenderEndTag(writer);
103
}
104
public bool LoadPostData(string postDataKey, NameValueCollection postCollection)
105
{
106
string oldText = this.Text;
107
string newText = postCollection[postDataKey];
108
if (!this.ReadOnly && !oldText.Equals(newText))
109
{
110
this.Text = newText;
111
return true;
112
}
113
return false;
114
}
115
public void OnTextChanged(EventArgs e)
116
{
117
EventHandler handler1 = (EventHandler)base.Events[InventTextBox.EventTextChanged];
118
if (handler1 != null)
119
{
120
handler1(this, e);
121
}
122
}
123
public void RaisePostDataChangedEvent()
124
{
125
OnTextChanged(EventArgs.Empty);
126
}
127
bool IPostBackDataHandler.LoadPostData(string postDataKey, NameValueCollection postCollection)
128
{
129
return this.LoadPostData(postDataKey, postCollection);
130
}
131
132
void IPostBackDataHandler.RaisePostDataChangedEvent()
133
{
134
this.RaisePostDataChangedEvent();
135
}
136
[Bindable(true), Category("Behavior")]
137
public string Text
138
{
139
get
140
{
141
string text = (string)ViewState["Text"];
142
if (text != null)
143
{
144
return text;
145
}
146
else
147
{
148
return string.Empty;
149
}
150
}
151
set
152
{
153
ViewState["Text"] = value;
154
}
155
}
156
[Bindable(true), Category("Behavior")]
157
public bool ReadOnly
158
{
159
get
160
{
161
object obj = ViewState["ReadOnly"];
162
if (obj != null)
163
{
164
return (bool)obj;
165
}
166
else
167
{
168
return false;
169
}
170
}
171
set
172
{
173
ViewState["ReadOnly"] = value;
174
}
175
}
176
[Bindable(true), Category("Behavior")]
177
public bool AutoPostBack
178
{
179
get
180
{
181
object obj = ViewState["AutoPostBack"];
182
if (obj != null)
183
{
184
return (bool)obj;
185
}
186
return false;
187
}
188
set
189
{
190
ViewState["AutoPostBack"] = value;
191
}
192
}
193
public TextBoxMode TextMode
194
{
195
get
196
{
197
object obj = ViewState["TextMode"];
198
if (obj != null)
199
{
200
return (TextBoxMode)obj;
201
}
202
return TextBoxMode.SingleLine;
203
}
204
set
205
{
206
ViewState["TextMode"] = value;
207
}
208
}
209
[Description("TextBox_Rows"), DefaultValue(0), Themeable(false), Category("Behavior")]
210
public virtual int Rows
211
{
212
get
213
{
214
object obj1 = this.ViewState["Rows"];
215
if (obj1 != null)
216
{
217
return (int)obj1;
218
}
219
return 0;
220
}
221
set
222
{
223
if (value < 0)
224
{
225
throw new ArgumentOutOfRangeException("Rows", "TextBox_InvalidRows");
226
}
227
this.ViewState["Rows"] = value;
228
}
229
}
230
[Description("TextBox_Columns"), Category("Appearance"), DefaultValue(0)]
231
public virtual int Columns
232
{
233
get
234
{
235
object obj1 = this.ViewState["Columns"];
236
if (obj1 != null)
237
{
238
return (int)obj1;
239
}
240
return 0;
241
}
242
set
243
{
244
if (value < 0)
245
{
246
throw new ArgumentOutOfRangeException("Columns","TextBox_InvalidColumns");
247
}
248
this.ViewState["Columns"] = value;
249
}
250
}
251
private static readonly object EventTextChanged;
252
public enum TextBoxMode
253
{
254
SingleLine,
255
MultiLine
256
};
257
}
258
259
}
260
using System;2
using System.Collections.Generic;3
using System.Text;4
using System.Web;5
using System.Drawing.Design;6
using System.Globalization;7
using System.Web.UI;8
using System.Web.UI.WebControls;9
using System.Security.Permissions;10
using System.Collections.Specialized;11
using System.ComponentModel;12
[assembly: TagPrefix("Invent.Controls", "Invent")]13
namespace InventControls14
{15
[ToolboxData("<{0}:InventTextBox runat=server></{0}:InventTextBox>")]16
public class InventTextBox : WebControl, IPostBackDataHandler, IEditableTextControl, ITextControl17
{18
public event EventHandler TextChanged19
{20
add21
{22
base.Events.AddHandler(InventTextBox.EventTextChanged, value);23
}24
remove25
{26
base.Events.RemoveHandler(InventTextBox.EventTextChanged, value);27
}28
}29

30
static InventTextBox()31
{32
InventTextBox.EventTextChanged = new object();33
}34

35
public InventTextBox() : base(HtmlTextWriterTag.Input)36
{37
}38
protected override void AddAttributesToRender(HtmlTextWriter writer)39
{40
Page page1 = this.Page;41
if (page1 != null)42
{43
page1.VerifyRenderingInServerForm(this);44
}45
string text1 = this.UniqueID;46
if (text1 != null)47
{48
writer.AddAttribute(HtmlTextWriterAttribute.Name, text1);49
}50
string text3 = this.Text;51
if (text3.Length > 0)52
{53
writer.AddAttribute(HtmlTextWriterAttribute.Value, text3);54
}55
if (this.ReadOnly)56
{57
writer.AddAttribute(HtmlTextWriterAttribute.ReadOnly, "readonly");58
}59
60
if (this.AutoPostBack && this.Page != null)61
{62
string text4 = null;63
if (base.HasAttributes)64
{65
text4 = base.Attributes["onchange"];66
if (text4 != null)67
{68
text4 = Util.EnsureEndWithSemiColon(text4);69
base.Attributes.Remove("onchange");70
}71
}72
PostBackOptions options1 = new PostBackOptions(this, string.Empty);73
if (this.Page.Form != null)74
{75
options1.AutoPostBack = true;76
text4 = Util.MergeScript(text4, page1.ClientScript.GetPostBackEventReference(options1, true));77
writer.AddAttribute(HtmlTextWriterAttribute.Onchange, text4);78
writer.AddAttribute("language", "javascript", false);79
}80
}81
else if (page1 != null)82
{83
page1.ClientScript.RegisterForEventValidation(this.UniqueID, string.Empty);84
}85
if (this.Enabled && !base.IsEnabled)86
{87
writer.AddAttribute(HtmlTextWriterAttribute.Disabled, "disabled");88
}89
base.AddAttributesToRender(writer);90
}91
protected override void OnPreRender(EventArgs e)92
{93
base.OnPreRender(e);94
}95
protected override void Render(HtmlTextWriter writer)96
{97
this.RenderBeginTag(writer);98
if (this.TextMode == TextBoxMode.MultiLine)99
{100
HttpUtility.HtmlEncode(this.Text, writer);101
}102
this.RenderEndTag(writer);103
}104
public bool LoadPostData(string postDataKey, NameValueCollection postCollection)105
{106
string oldText = this.Text;107
string newText = postCollection[postDataKey];108
if (!this.ReadOnly && !oldText.Equals(newText))109
{110
this.Text = newText;111
return true;112
}113
return false;114
}115
public void OnTextChanged(EventArgs e)116
{117
EventHandler handler1 = (EventHandler)base.Events[InventTextBox.EventTextChanged];118
if (handler1 != null)119
{120
handler1(this, e);121
}122
}123
public void RaisePostDataChangedEvent()124
{125
OnTextChanged(EventArgs.Empty);126
}127
bool IPostBackDataHandler.LoadPostData(string postDataKey, NameValueCollection postCollection)128
{129
return this.LoadPostData(postDataKey, postCollection);130
}131

132
void IPostBackDataHandler.RaisePostDataChangedEvent()133
{134
this.RaisePostDataChangedEvent();135
}136
[Bindable(true), Category("Behavior")]137
public string Text138
{139
get140
{141
string text = (string)ViewState["Text"];142
if (text != null)143
{144
return text;145
}146
else147
{148
return string.Empty;149
}150
}151
set152
{153
ViewState["Text"] = value;154
}155
}156
[Bindable(true), Category("Behavior")]157
public bool ReadOnly158
{159
get160
{161
object obj = ViewState["ReadOnly"];162
if (obj != null)163
{164
return (bool)obj;165
}166
else167
{168
return false;169
}170
}171
set172
{173
ViewState["ReadOnly"] = value;174
}175
}176
[Bindable(true), Category("Behavior")]177
public bool AutoPostBack178
{179
get180
{181
object obj = ViewState["AutoPostBack"];182
if (obj != null)183
{184
return (bool)obj;185
}186
return false;187
}188
set189
{190
ViewState["AutoPostBack"] = value;191
}192
}193
public TextBoxMode TextMode194
{195
get196
{197
object obj = ViewState["TextMode"];198
if (obj != null)199
{200
return (TextBoxMode)obj;201
}202
return TextBoxMode.SingleLine;203
}204
set205
{206
ViewState["TextMode"] = value;207
}208
}209
[Description("TextBox_Rows"), DefaultValue(0), Themeable(false), Category("Behavior")]210
public virtual int Rows211
{212
get213
{214
object obj1 = this.ViewState["Rows"];215
if (obj1 != null)216
{217
return (int)obj1;218
}219
return 0;220
}221
set222
{223
if (value < 0)224
{225
throw new ArgumentOutOfRangeException("Rows", "TextBox_InvalidRows");226
}227
this.ViewState["Rows"] = value;228
}229
}230
[Description("TextBox_Columns"), Category("Appearance"), DefaultValue(0)]231
public virtual int Columns232
{233
get234
{235
object obj1 = this.ViewState["Columns"];236
if (obj1 != null)237
{238
return (int)obj1;239
}240
return 0;241
}242
set243
{244
if (value < 0)245
{246
throw new ArgumentOutOfRangeException("Columns","TextBox_InvalidColumns");247
}248
this.ViewState["Columns"] = value;249
}250
}251
private static readonly object EventTextChanged;252
public enum TextBoxMode253
{254
SingleLine,255
MultiLine256
};257
}258
259
}260


浙公网安备 33010602011771号