酷徒LOGO

自定义网页控件库


介绍

这里库包含四个自定义控件。 每个控件都具有所需字符类型的验证:

  • NumericTextBox

这里控件对输入的char 进行验证,并且只接受数字。

  • DecimalTextBox

这里控件对输入的char 进行验证,并且只接受十进制。

  • StringTextBox

这里控件在按键上验证输入的char,并接受alpha脉冲。 根据它的属性 AllowSpaceAllowDigit,它将接受数字和空格。

  • CustomTextBox

这里控件具有类型属性,并且根据它在验证验证时将在提交之前发生。

使用代码

拖动页面上的控件后,只需设置控件属性:

<cc1:StringTextBoxID="StringTextBox1"runat="server"AllowDigit="true"AllowSpace="true">cc1:CustomTextBox ID="CustomTextBox6"
 runat="server" TextType="AlphaWithSpace" InvalidRequiredMessage="*"> 

所有这些控件都有公共属性,它们包括在 BaseCustomControlInvalidRequiredMessageEnableClientScriptIsRequiredCssClassTxtCssClassErr

源代码

1- BaseCustomControl

publicclass BaseCustomControl : TextBox
 {
 publicstring InvalidRequiredMessage
 {
 get {
 string s = "Required";
 object obj = ViewState["InvalidRequiredMessage"];
 if (obj!= null)
 {
 s = (string)obj;
 }
 return s;
 }
 set {
 ViewState["InvalidRequiredMessage"] = value;
 }
 }
 publicbool EnableClientScript
 {
 get {
 bool b = true;
 object obj = ViewState["EnableClientScript"];
 if (obj!= null)
 {
 b = (bool)obj;
 }
 return b;
 }
 set {
 ViewState["EnableClientScript"] = value;
 }
 }
 publicbool IsRequired
 {
 get {
 bool b = true;
 object obj = ViewState["IsRequired"];
 if (obj!= null)
 {
 b = (bool)obj;
 }
 return b;
 }
 set {
 ViewState["IsRequired"] = value;
 }
 }
 publicstring CssClassTxt
 {
 get {
 string s = "txtCls";
 object obj = ViewState["CssClassTxt"];
 if (obj!= null)
 {
 s = (string)obj;
 }
 return s;
 }
 set {
 ViewState["CssClassTxt"] = value;
 this.CssClass = value;
 }
 }
 publicstring CssClassErr
 {
 get {
 string s = "errCls";
 object obj = ViewState["CssClassErr"];
 if (obj!= null)
 {
 s = (string)obj;
 }
 return s;
 }
 set {
 ViewState["CssClassErr"] = value;
 }
 }
 protectedoverridevoid OnInit(EventArgs e)
 {
 //check Requiredif (IsRequired)
 {
 req = new RequiredFieldValidator();
 req.ControlToValidate = this.ID;
 req.ErrorMessage = this.InvalidRequiredMessage;
 req.EnableClientScript = this.EnableClientScript;
 req.Display = ValidatorDisplay.Dynamic;
 req.CssClass = CssClassErr;
 Controls.Add(req);
 }
 CssClass = CssClassTxt;
 }
 protectedoverridevoid Render(HtmlTextWriter output)
 {base.Render(output);
 if (req!= null)
 {
 req.RenderControl(output);
 }
 }
}

2- CustomTextBox

publicenum TextBoxType
 {
 Text = 1,
 AlphaWithSpace = 2,
 AlphaOnly=3,
 Numeric=4,
 Email = 5,
 URL=6 }
 publicclass CustomTextBox : BaseCustomControl
 {
 publicstring InvalidDataMessage
 {
 get {string s = "Invalid Data";object obj = ViewState["InvalidDataMessage"];if (obj!= null)
{
 s = (string)obj;
}return s;
 }
 set {
ViewState["InvalidDataMessage"] = value;
 }
 }
 public TextBoxType TextType
 {
 get {
TextBoxType t = TextBoxType.AlphaOnly;object obj = ViewState["TextType"];if (obj!= null)
{
 t = (TextBoxType)obj;
}return t;
 }
 set {
ViewState["TextType"] = value;
 }
 protectedoverridevoid OnInit(EventArgs e)
 {
 base.OnInit(e);
 reg = new RegularExpressionValidator();
 reg.ControlToValidate = this.ID;
 reg.ErrorMessage = this.InvalidDataMessage;
 reg.EnableClientScript = this.EnableClientScript;
 reg.Display = ValidatorDisplay.Dynamic;
 //check Typeswitch (TextType)
 {case TextBoxType.AlphaOnly:
 {
 reg.ValidationExpression = @"[a-zA-Z]*";
 break;
 }case TextBoxType.Numeric:
 {
 reg.ValidationExpression = @"[0-9]*";
 break;
 }case TextBoxType.AlphaWithSpace:
 {
 reg.ValidationExpression = @"[a-zA-Z ]*";
 break;
 }case TextBoxType.Text:
 {
 reg.ValidationExpression = @"[w^wW^W]*";
 break;
 }case TextBoxType.Email:
 {
 reg.ValidationExpression = @"w+([-+.']w+)*@w+([-.]w+)*.w+([-.]w+)*";
 break;
 }
 case TextBoxType.URL:
 {
 reg.ValidationExpression = @"http(s)?://([w-]+.)+[w-]+(/[w-./?%&=]*)?";
 break;
 }
 }
 Controls.Add(reg);
 #endregion }
 protectedoverridevoid Render(HtmlTextWriter w)
 {base.Render(w);if (reg!= null)
{
 reg.RenderControl(w);
}
 }

3- DecimalTextBox

publicclass DecimalTextBox : BaseCustomControl
 {
 protectedoverridevoid OnInit(EventArgs e)
 {
 this.Attributes.Add("onkeypress", "javascript: return AllowDecimal
 (event,'"+this.ClientID+"');");
 base.OnInit(e);
 }
 protectedoverridevoid OnPreRender(EventArgs e)
{f(!this.Page.ClientScript.IsClientScriptBlockRegistered("strAllowNumeric"))
 {
 string script = @"function AllowDecimal(e,txt)
 {
 var iKeyCode = 0;
 if (window.event)
 {
 iKeyCode = window.event.keyCode
 }
 else if (e)
 {
 iKeyCode = e.which;
 }
 var y = document.getElementById(txt).value;
 if(iKeyCode==46 && y.indexOf('.')==-1){ return true;}
 else if ((iKeyCode> 47 && iKeyCode <58)|| iKeyCode==8)
 { return true;}
 else
 {return false;} };
this.Page.ClientScript.RegisterClientScriptBlock
 (this.Page.GetType(),"strAllowDecimal", script,true);
 }
 base.OnPreRender(e);
 }
 }
}

4- NumericTextBox

publicclass NumericTextBox : BaseCustomControl
 {
 protectedoverridevoid OnInit(EventArgs e)
 { 
 this.Attributes.Add("onkeypress", 
 "javascript: return AllowNumeric(event);");
 base.OnInit(e); 
 protectedoverridevoid OnPreRender(EventArgs e)
{if(!this.Page.ClientScript.IsClientScriptBlockRegistered("strAllowNumeric"))
 {
 string script = "function AllowNumeric(e)";
 script += "{";
 script += "var iKeyCode = 0;";
 script += "if (window.event)";
 script += "{iKeyCode = window.event.keyCode}";
 script += "else if (e)";
 script += "{iKeyCode = e.which;}";
 script += "if ((iKeyCode> 47 && iKeyCode <58) || 
 iKeyCode == 8||iKeyCode == 0)";
 script += "{ return true}";
 script += "else";
 script += "{return false;}";
 script += "}";this.Page.ClientScript.RegisterClientScriptBlock(this.Page.GetType(), 
 "strAllowNumeric", script,true); 
 }
 base.OnPreRender(e);
 }
 catch (Exception ex)
 {
 throw ex;
 }
 }
 #endregion }
}

5- StringTextBox

publicclass StringTextBox : BaseCustomControl
 {
 publicbool AllowAlpha
 {
 get {
 bool b = true;
 object obj = ViewState["AllowAlpha"];
 if (obj!= null)
 {
 b = (bool)obj;
 }
 return b;
 }
 set {
 ViewState["AllowAlpha"] = value;
 }
 }publicbool AllowSpace
 {
 get {
 bool b = false;
 object obj = ViewState["AllowSpace"];
 if (obj!= null)
 {
 b = (bool)obj;
 }
 return b;
 }
 set {
 ViewState["AllowSpace"] = value;
 }
 }
 publicbool AllowDigit
 {
 get {
 bool b = false;
 object obj = ViewState["AllowDigit"];
 if (obj!= null)
 {
 b = (bool)obj;
 }
 return b;
 }
 set {
 ViewState["AllowDigit"] = value;
 }
 }
 protectedoverridevoid OnInit(EventArgs e)
 {
 base.OnInit(e);
 if ((!AllowAlpha) && AllowDigit)
 {
 this.Attributes.Add("onkeypress", "javascript: return AllowDigit
 (event," + AllowSpace.ToString().ToLower() + ");");
 }
 elseif (AllowAlpha)
 {
 this.Attributes.Add("onkeypress", "javascript: return AllowAlpha
 (event," + AllowSpace.ToString().ToLower() + "," + 
 AllowDigit.ToString().ToLower() + ");");
 }
 else {
 this.Enabled = false;
 }
 protectedoverridevoid OnPreRender(EventArgs e)
 {string script1="<script language="javascript">";
 script1+="function AllowAlpha(e,allowSpace,allowDigit)";
 script1+="{";
 script1+="var KeyID = (window.event)? event.keyCode : e.which;";
 script1+="if((KeyID> = 65 && KeyID <= 90)";
 script1+="|| (KeyID> = 97 && KeyID <= 122)";
 script1+="|| (KeyID> = 33 && KeyID <= 47)";
 script1+="||(KeyID> = 58 && KeyID <= 64)";
 script1+="|| (KeyID> = 91 && KeyID <= 96)";
 script1+="|| (KeyID ==32 && allowSpace)";
 script1+="|| (KeyID> 47 && KeyID <58 && allowDigit)";
 script1+="|| (KeyID> = 123 && KeyID <= 126))";
 script1+="{return true;}";
 script1+="else";
 script1+="{return false;}";
 script1+="}";
 script1+="</script>";
 this.Page.ClientScript.RegisterClientScriptBlock(this.Page.GetType(), 
 "strAllowAlpha", script1);
 if ((!AllowAlpha) && AllowDigit)
 {
 string script="<script language="javascript">";
 script+="function AllowDigit(e,allowSpace)";
 script+="{";
 script+="var iKeyCode = 0;";
 script+="if (window.event)";
 script+="{iKeyCode = window.event.keyCode}";
 script+="else if (e)";
 script+="{iKeyCode = e.which;}";
 script+="if ((iKeyCode> 47 && iKeyCode <58)";
 script+="|| (iKeyCode ==32 && allowSpace))";
 script+="{ return true}";
 script+="else";
 script+="{return false;}";
 script+="}";
 script += "</script>";
 this.Page.ClientScript.RegisterClientScriptBlock(this.Page.GetType(), 
 "strAllowDigit", script);
 }
 base.OnPreRender(e);
 }

历史记录

  • 22nd 2009年05月: 初始帖子




Copyright © 2011 HelpLib All rights reserved.    知识分享协议 京ICP备17041772号-2  |  如果智培  |  酷兔英语  |  帮酷