开发的时候,TextBox是个不得不用,又经常用的东西,经常会遇上是否允许空,数据类型的判断等等,用脚本吧,麻烦;用验证控件吧,也麻烦。自己写了一个,使用的时候选择下属性就OK了,还支持自定义正则表达式验证。以此作为在此的开门之笔吧:)
开发的时候,TextBox是个不得不用,又经常用的东西,经常会遇上是否允许空,数据类型的判断等等,用脚本吧,麻烦;用验证控件吧,也麻烦。自己写了一个,使用的时候选择下属性就OK了,还支持自定义正则表达式验证。以此作为在此的开门之笔吧:)
1
using System;
2
using System.Web.UI;
3
using System.Web.UI.WebControls;
4
using System.ComponentModel;
5
using System.Drawing;
6
using System.Text;
7
using System.Text.RegularExpressions;
8![](https://www.cnblogs.com/Images/OutliningIndicators/None.gif)
9
namespace rungoo.WebCtrlLib
10![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedBlockStart.gif)
![](https://www.cnblogs.com/Images/OutliningIndicators/ContractedBlock.gif)
{
11![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
/**//// <summary>
12
/// 重写的TextBox控件
13
/// Author: nowind
14
/// Date: 2006-5-9
15
/// Email: hgh113@sina.com
16
/// QQ: 87505959
17
/// <summary>
18
///
19![](https://www.cnblogs.com/Images/OutliningIndicators/ContractedSubBlock.gif)
枚举数据#region 枚举数据
20![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
/**//// <summary>
21
/// 验证数据类型
22
/// </summary>
23
public enum DataType
24![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
25
Never, //不验证
26
String, //字符串
27
Int, //整数
28
IntPostive, //大于0的整数
29
IntZeroPostive, //大于等于0的整数
30
Float, //数字
31
FloatPostive, //大于0的数字
32
FloatZeroPostive, //大于等于0的数字
33
Url,
34
Mail,
35
//ChineseChars, //汉字
36
EnglishChars, //英文
37
EngNum, //英文和数字
38
EngNumUnerline, //英文、数字和下划线
39
PhoneNumber, //电话号码
40
MobileNumber, //手机号码
41
PostalCode, //邮政编码
42
Custom
43
}
44
#endregion
45![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
46![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
/**//// Attribute DefaultProperty指定组件的默认属性,ToolboxData指定当从IDE工具中的工具箱中拖动自定义控件时为它生成的默认标记
47
[DefaultProperty("AllowEmpty"),ToolboxData("<{0}:WebTextBox runat=server></{0}:WebTextBox>")]
48![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
49
//类MyControl派生自WebControl
50
public class WebTextBox : System.Web.UI.WebControls.TextBox
51![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
52![](https://www.cnblogs.com/Images/OutliningIndicators/ContractedSubBlock.gif)
子控件#region 子控件
53
//private System.Web.UI.WebControls.TextBox txtDataInput = new TextBox();
54
private System.Web.UI.WebControls.RequiredFieldValidator rfvDataInput = new RequiredFieldValidator();
55
private System.Web.UI.WebControls.RegularExpressionValidator revDataInput = new RegularExpressionValidator();
56
private Panel pnlFrame = new Panel(); //承载其它控件的容器Panel控件
57
#endregion
58![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
59
private string error = "";
60![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
61![](https://www.cnblogs.com/Images/OutliningIndicators/ContractedSubBlock.gif)
控件自定义属性#region 控件自定义属性
62![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
63
[Bindable(true)]
64
[Category("自定义信息区")]
65
[Browsable(true)]
66
[Description("是否允许空值")]
67
[DefaultValue("true")]
68
public bool AllowEmpty
69![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
70![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
get
{ return ViewState["AllowEmpty"] == null ? true : (bool)ViewState["AllowEmpty"] ; }
71![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
set
{ ViewState["AllowEmpty"] = value; }
72
}
73![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
74
[Bindable(true)]
75
[Category("自定义信息区")]
76
[ Browsable(true)]
77
[Description("验证数据类型,默认为不验证")]
78
[DefaultValue("IntPostive")]
79
public DataType ValidType
80![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
81![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
get
{ return ViewState["ValidType"] == null ? DataType.Never : (DataType)ViewState["ValidType"] ;}
82![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
set
{ ViewState["ValidType"] = value; }
83
}
84![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
85
[Bindable(true)]
86
[Browsable(true)]
87
[Category("自定义信息区")]
88
[Description("自定义验证错误信息")]
89
[DefaultValue("")]
90
public string ValidError
91![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
92![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
get
{ return ViewState["ValidError"] == null ? "" : (string)ViewState["ValidError"] ; }
93![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
set
{ ViewState["ValidError"] = value; }
94
}
95![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
96
[Bindable(true)]
97
[Browsable(true)]
98
[Category("自定义信息区")]
99
[Description("自定义用于验证的正则表达式,ValidType 为 Custom 时有效")]
100
[DefaultValue("")]
101
public string ValidExpressionCustom
102![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
103![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
get
{ return ViewState["ValidExpressionCustom"] == null ? "" : (string)ViewState["ValidExpressionCustom"] ; }
104![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
set
{ ViewState["ValidExpressionCustom"] = value; }
105
}
106![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
107
[Bindable(true)]
108
[Browsable(true)]
109
[Category("自定义信息区")]
110
[Description("错误信息提示的CSS类名")]
111
[DefaultValue("")]
112
public string CssError
113![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
114![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
get
{ return ViewState["CssError"] == null ? "" : (string)ViewState["CssError"] ; }
115![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
set
{ ViewState["CssError"] = value; }
116
}
117![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
118
#endregion
119![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
120![](https://www.cnblogs.com/Images/OutliningIndicators/ContractedSubBlock.gif)
构造函数#region 构造函数
121![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
public WebTextBox()
{}
122
#endregion
123![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
124![](https://www.cnblogs.com/Images/OutliningIndicators/ContractedSubBlock.gif)
EnsureChildControls#region EnsureChildControls
125
protected override void EnsureChildControls()
126![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
127
this.rfvDataInput.CssClass = this.CssError;
128
this.rfvDataInput.ErrorMessage = "*输入不能为空";
129
this.rfvDataInput.Display = System.Web.UI.WebControls.ValidatorDisplay.Dynamic;
130
this.rfvDataInput.EnableViewState = true;
131
this.rfvDataInput.ControlToValidate = base.ID;
132![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
133
this.revDataInput.CssClass = this.CssError;
134
this.revDataInput.ErrorMessage = "*输入格式错误";
135
this.revDataInput.Display = System.Web.UI.WebControls.ValidatorDisplay.Dynamic;
136
this.revDataInput.EnableViewState = true;
137
this.revDataInput.ControlToValidate = base.ID;
138![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
139
//将子控件添加到此自定义控件中
140
this.Controls.Add(rfvDataInput);
141
this.Controls.Add(revDataInput);
142
this.Controls.Add(pnlFrame);
143
}
144
#endregion
145![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
146![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
/**//// <summary>
147
/// 根据设置的验证数据类型返回不同的正则表达式样
148
/// </summary>
149
/// <returns></returns>
150![](https://www.cnblogs.com/Images/OutliningIndicators/ContractedSubBlock.gif)
GetRegex#region GetRegex
151
private string GetValidRegex()
152![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
153
string regex = @"(\S)";
154
switch( this.ValidType )
155![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
156
case DataType.Never :
157
break;
158
case DataType.Int :
159
error = "*必须为整数";
160
regex = @"(-)?(\d+)";
161
break;
162
case DataType.IntPostive :
163
error = "*必须为大于0的整数";
164
regex = @"([1-9]{1}\d*)";
165
break;
166
case DataType.IntZeroPostive :
167
error = "*必须为不小于0的整数";
168
regex = @"(\d+)";
169
break;
170
case DataType.Float :
171
error = "*必须为数字";
172
regex = @"(-)?(\d+)(((\.)(\d)+))?";
173
break;
174
case DataType.FloatPostive :
175
error = "*必须为大于0的数字";
176
regex = @"(\d+)(((\.)(\d)+))?";
177
break;
178
case DataType.FloatZeroPostive :
179
error = "*必须为不小于0的数字";
180
regex = @"(\d+)(((\.)(\d)+))?";
181
break;
182
case DataType.Url :
183
error = "*URL格式错误";
184
regex = @"(http://)?([\w-]+\.)+[\w-]+(/[\w- ./?%&=]*)?";
185
break;
186
case DataType.Mail :
187
error = "*EMail格式错误";
188
regex = @"\w+([-+.]\w+)*@\w+([-.]\w+)*\.\w+([-.]\w+)*";
189
break;
190
// case DataType.ChineseChars :
191
// error = "*包含中文字符";
192
// regex = @"[^\x00-\xff]";
193
// break;
194
case DataType.EnglishChars :
195
error = "*只能输入英文字符";
196
regex = @"[a-zA-Z]*";
197
break;
198
case DataType.EngNum :
199
error = "*只能输入英文字符和数字";
200
regex = @"[a-zA-Z0-9]*";
201
break;
202
case DataType.EngNumUnerline :
203
error = "*只能输入英文字符、数字和下划线";
204
regex = @"[a-zA-Z0-9_]*";
205
break;
206
case DataType.PhoneNumber :
207
error = "*电话号码格式错误";
208
regex = @"(86)?(-)?(0\d{2,3})?(-)?(\d{7,8})(-)?(\d{1,5})?";
209
break;
210
case DataType.MobileNumber :
211
error = "*手机号码格式错误";
212
regex = @"(0)?(13)\d{9}";
213
break;
214
case DataType.PostalCode :
215
error = "*邮编格式错误";
216
regex = @"\d{6}";
217
break;
218
case DataType.Custom :
219
error = "*格式错误";
220
regex = this.ValidExpressionCustom;
221
break;
222
default:
223
break;
224
}
225
if(this.ValidError.Trim() != "")
226
error = this.ValidError;
227
return regex;
228
}
229
#endregion
230![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
231![](https://www.cnblogs.com/Images/OutliningIndicators/ContractedSubBlock.gif)
将此控件呈现给指定的输出参数#region 将此控件呈现给指定的输出参数
232![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
/**//// <summary>
233
/// 将此控件呈现给指定的输出参数。
234
/// </summary>
235
/// <param name="output"> 要写出到的 HTML 编写器 </param>
236
protected override void Render(HtmlTextWriter output)
237![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
238
base.Render(output);
239
output.Write(" ");
240![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
241
if( !this.AllowEmpty )
242![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
243
this.rfvDataInput.ID = "rfv" + base.ID;
244
this.rfvDataInput.ControlToValidate = base.ID;
245
this.rfvDataInput.RenderControl(output);
246
}
247![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
248
if( this.ValidType != DataType.Never && this.ValidType != DataType.String )
249![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
250
this.revDataInput.ID = "rev" + base.ID;
251
this.revDataInput.ControlToValidate = base.ID;
252
this.revDataInput.ValidationExpression = this.GetValidRegex();
253
this.revDataInput.ErrorMessage = error;
254
this.revDataInput.RenderControl(output);
255
}
256
}
257
#endregion
258![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
259
}
260
}
261![](https://www.cnblogs.com/Images/OutliningIndicators/None.gif)
262![](https://www.cnblogs.com/Images/OutliningIndicators/None.gif)