开发的时候,TextBox是个不得不用,又经常用的东西,经常会遇上是否允许空,数据类型的判断等等,用脚本吧,麻烦;用验证控件吧,也麻烦。自己写了一个,使用的时候选择下属性就OK了,还支持自定义正则表达式验证。以此作为在此的开门之笔吧:)
1using System;
2using System.Web.UI;
3using System.Web.UI.WebControls;
4using System.ComponentModel;
5using System.Drawing;
6using System.Text;
7using System.Text.RegularExpressions;
8
9namespace rungoo.WebCtrlLib
10{
11 /**//// <summary>
12 /// 重写的TextBox控件
13 /// Author: nowind
14 /// Date: 2006-5-9
15 /// Email: hgh113@sina.com
16 /// QQ: 87505959
17 /// <summary>
18 ///
19 枚举数据#region 枚举数据
20 /**//// <summary>
21 /// 验证数据类型
22 /// </summary>
23 public enum DataType
24 {
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
46 /**//// Attribute DefaultProperty指定组件的默认属性,ToolboxData指定当从IDE工具中的工具箱中拖动自定义控件时为它生成的默认标记
47 [DefaultProperty("AllowEmpty"),ToolboxData("<{0}:WebTextBox runat=server></{0}:WebTextBox>")]
48
49 //类MyControl派生自WebControl
50 public class WebTextBox : System.Web.UI.WebControls.TextBox
51 {
52 子控件#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
59 private string error = "";
60
61 控件自定义属性#region 控件自定义属性
62
63 [Bindable(true)]
64 [Category("自定义信息区")]
65 [Browsable(true)]
66 [Description("是否允许空值")]
67 [DefaultValue("true")]
68 public bool AllowEmpty
69 {
70 get { return ViewState["AllowEmpty"] == null ? true : (bool)ViewState["AllowEmpty"] ; }
71 set { ViewState["AllowEmpty"] = value; }
72 }
73
74 [Bindable(true)]
75 [Category("自定义信息区")]
76 [ Browsable(true)]
77 [Description("验证数据类型,默认为不验证")]
78 [DefaultValue("IntPostive")]
79 public DataType ValidType
80 {
81 get { return ViewState["ValidType"] == null ? DataType.Never : (DataType)ViewState["ValidType"] ;}
82 set { ViewState["ValidType"] = value; }
83 }
84
85 [Bindable(true)]
86 [Browsable(true)]
87 [Category("自定义信息区")]
88 [Description("自定义验证错误信息")]
89 [DefaultValue("")]
90 public string ValidError
91 {
92 get { return ViewState["ValidError"] == null ? "" : (string)ViewState["ValidError"] ; }
93 set { ViewState["ValidError"] = value; }
94 }
95
96 [Bindable(true)]
97 [Browsable(true)]
98 [Category("自定义信息区")]
99 [Description("自定义用于验证的正则表达式,ValidType 为 Custom 时有效")]
100 [DefaultValue("")]
101 public string ValidExpressionCustom
102 {
103 get { return ViewState["ValidExpressionCustom"] == null ? "" : (string)ViewState["ValidExpressionCustom"] ; }
104 set { ViewState["ValidExpressionCustom"] = value; }
105 }
106
107 [Bindable(true)]
108 [Browsable(true)]
109 [Category("自定义信息区")]
110 [Description("错误信息提示的CSS类名")]
111 [DefaultValue("")]
112 public string CssError
113 {
114 get { return ViewState["CssError"] == null ? "" : (string)ViewState["CssError"] ; }
115 set { ViewState["CssError"] = value; }
116 }
117
118 #endregion
119
120 构造函数#region 构造函数
121 public WebTextBox(){}
122 #endregion
123
124 EnsureChildControls#region EnsureChildControls
125 protected override void EnsureChildControls()
126 {
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
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
139 //将子控件添加到此自定义控件中
140 this.Controls.Add(rfvDataInput);
2using System.Web.UI;
3using System.Web.UI.WebControls;
4using System.ComponentModel;
5using System.Drawing;
6using System.Text;
7using System.Text.RegularExpressions;
8
9namespace rungoo.WebCtrlLib
10{
11 /**//// <summary>
12 /// 重写的TextBox控件
13 /// Author: nowind
14 /// Date: 2006-5-9
15 /// Email: hgh113@sina.com
16 /// QQ: 87505959
17 /// <summary>
18 ///
19 枚举数据#region 枚举数据
20 /**//// <summary>
21 /// 验证数据类型
22 /// </summary>
23 public enum DataType
24 {
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
46 /**//// Attribute DefaultProperty指定组件的默认属性,ToolboxData指定当从IDE工具中的工具箱中拖动自定义控件时为它生成的默认标记
47 [DefaultProperty("AllowEmpty"),ToolboxData("<{0}:WebTextBox runat=server></{0}:WebTextBox>")]
48
49 //类MyControl派生自WebControl
50 public class WebTextBox : System.Web.UI.WebControls.TextBox
51 {
52 子控件#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
59 private string error = "";
60
61 控件自定义属性#region 控件自定义属性
62
63 [Bindable(true)]
64 [Category("自定义信息区")]
65 [Browsable(true)]
66 [Description("是否允许空值")]
67 [DefaultValue("true")]
68 public bool AllowEmpty
69 {
70 get { return ViewState["AllowEmpty"] == null ? true : (bool)ViewState["AllowEmpty"] ; }
71 set { ViewState["AllowEmpty"] = value; }
72 }
73
74 [Bindable(true)]
75 [Category("自定义信息区")]
76 [ Browsable(true)]
77 [Description("验证数据类型,默认为不验证")]
78 [DefaultValue("IntPostive")]
79 public DataType ValidType
80 {
81 get { return ViewState["ValidType"] == null ? DataType.Never : (DataType)ViewState["ValidType"] ;}
82 set { ViewState["ValidType"] = value; }
83 }
84
85 [Bindable(true)]
86 [Browsable(true)]
87 [Category("自定义信息区")]
88 [Description("自定义验证错误信息")]
89 [DefaultValue("")]
90 public string ValidError
91 {
92 get { return ViewState["ValidError"] == null ? "" : (string)ViewState["ValidError"] ; }
93 set { ViewState["ValidError"] = value; }
94 }
95
96 [Bindable(true)]
97 [Browsable(true)]
98 [Category("自定义信息区")]
99 [Description("自定义用于验证的正则表达式,ValidType 为 Custom 时有效")]
100 [DefaultValue("")]
101 public string ValidExpressionCustom
102 {
103 get { return ViewState["ValidExpressionCustom"] == null ? "" : (string)ViewState["ValidExpressionCustom"] ; }
104 set { ViewState["ValidExpressionCustom"] = value; }
105 }
106
107 [Bindable(true)]
108 [Browsable(true)]
109 [Category("自定义信息区")]
110 [Description("错误信息提示的CSS类名")]
111 [DefaultValue("")]
112 public string CssError
113 {
114 get { return ViewState["CssError"] == null ? "" : (string)ViewState["CssError"] ; }
115 set { ViewState["CssError"] = value; }
116 }
117
118 #endregion
119
120 构造函数#region 构造函数
121 public WebTextBox(){}
122 #endregion
123
124 EnsureChildControls#region EnsureChildControls
125 protected override void EnsureChildControls()
126 {
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
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
139 //将子控件添加到此自定义控件中
140 this.Controls.Add(rfvDataInput);