【IT168技术文档】
TextBox是用的比较多的控件,有时候我们希望用户的输入内容和符合我们的输入要求,按这个想法在原有控件的基础上,做了个能控制输入的TextBox,有兴趣的可以用用.原码如下.
1using System; 2using System.Collections.Generic; 3using System.Text; 4using System.ComponentModel; 5using System.Web.UI; 6using System.Web.UI.WebControls; 7using System.Drawing; 8namespace Jsl.Web.UI.WebControls 9{ 10 /**//// <summary> 11 /// 封装的TextBox,可以设置为文本框,整数文本框,浮点数文本框 12 /// 13 /// </summary> 14 public class JslTextBox : TextBox 15 { 16 17 18 IWebControl 成员#region IWebControl 成员 19 /**//// <summary> 20 /// 是否以文本方式显示 21 22 /// </summary> 23 /// 24 [Bindable(true)] 25 [Category("JSLProperty")] 26 [DefaultValue(false)] 27 [Localizable(true)] 28 [DescriptionAttribute("是否以文本方式显示")] 29 public bool ShowAsLabel 30 { 31 get 32 { 33 if (ViewState["ShowAsLabel"] != null) 34 return (bool)ViewState["ShowAsLabel"]; 35 return false; 36 } 37 set 38 { 39 ViewState["ShowAsLabel"] = value; 40 } 41 } 42 /**//// <summary> 43 /// 是否开启输入控制 44 45 /// </summary> 46 [Bindable(true)] 47 [Category("JSLProperty")] 48 [DefaultValue(false)] 49 [Localizable(true)] 50 [DescriptionAttribute("是否开启输入控制.")] 51 public bool Pree 52 { 53 get 54 { 55 56 object obj1 = this.ViewState["#pree#"]; 57 if (obj1 != null) 58 { 59 return (bool)obj1; 60 } 61 return false; 62 } 63 set 64 { 65 ViewState["#pree#"] = value; 66 } 67 } 68 /**//// <summary> 69 /// 控制类型 70 /// </summary> 71 [Bindable(true)] 72 [Category("JSLProperty")] 73 [DefaultValue(PreeClass.只能输入英文和中文)] 74 [Localizable(true)] 75 [DescriptionAttribute("控制类型.")] 76 public PreeClass PreeType 77 { 78 get 79 { 80 81 object obj1 = this.ViewState["#preetype#"]; 82 if (obj1 != null) 83 { 84 return (PreeClass)obj1; 85 } 86 return PreeClass.只能输入中文; 87 } 88 set 89 { 90 ViewState["#preetype#"] = value; 91 } 92 } 93 94 /**//// <summary> 95 /// 载入 96 /// </summary> 97 /// <param name="e"></param> 98 protected override void OnLoad(EventArgs e) 99 { 100 101 base.OnLoad(e); 102 if (Pree) 103 { 104 string script = ""; 105 switch(PreeType.ToString()) 106 { 107 case "只能输入英文和中文": 108 script = @"value=value.replace(/[ -/:-@\[-`0-9{-~]/, '')"; 109 break; 110 case "只能输入中文": 111 script = "value=value.replace(/[0-9a-zA-Z_!-|]/g, '')"; 112 break; 113 case "禁止输入中文": 114 script = "value=value.replace(/^[\u4E00-\u9FA5]+$/, '')"; 115 break; 116 case "禁止输入数字": 117 script = "value=value.replace(/[0-9]/g, '')"; 118 break; 119 case "只能输入数字": 120 script = "value=value.replace(/[^0-9_]/ig, '')"; 121 break; 122 case "禁止输入英文": 123 script = "value=value.replace(/[A-Za-z]/g, '')"; 124 break; 125 case "只能输入英文": 126 script = "value=value.replace(/[^a-zA-Z_]/ , '')"; 127 break; 128 129 } 130 131 this.Attributes.Add("onkeyup", script); 132 } 133 } 134 #endregion 135 136 137 138 139 140 } 141 142 /**//// <summary> 143 /// 文本框对象 144 145 /// </summary> 146 public enum PreeClass 147 { 148 /**//// <summary> 149 /// 只能输入英文和中文 150 151 /// </summary> 152 只能输入英文和中文 = 0, 153 /**//// <summary> 154 /// 只能输入中文 155 /// </summary> 156 只能输入中文 = 1, 157 /**//// <summary> 158 /// 禁止输入中文 159 /// </summary> 160 禁止输入中文 = 2, 161 /**//// <summary> 162 /// 禁止输入数字 163 /// </summary> 164 禁止输入数字 = 3, 165 /**//// <summary> 166 /// 只能输入数字 167 /// </summary> 168 只能输入数字 = 4, 169 /**//// <summary> 170 /// 禁止输入英文 171 /// </summary> 172 禁止输入英文 = 5, 173 /**//// <summary> 174 /// 只能输入英文 175 /// </summary> 176 只能输入英文 = 6 177 } 178}