<summary>///textbox Maximum length validator, if it is a Varchar,char field type, this verification is required///</summary> [Description ("Verify MaxLength" ] [ToolBoxData ("<{0}:maxlengthvalidator runat=server></{0}:maxlengthvalidator>")] public class Maxlengthvalidator:basevalidator {//<summary>////Set whether to validate for non-Nvarchar,nchar fields///</summary> [ DefaultValue (TRUE)] public bool Validatefornonnationalfield {get {var data = this. viewstate["Validatefornonnationalfield"]; if (data = = NULL) {return true;} else {return (bool) data;}} set {this. viewstate["Validatefornonnationalfield"] = value; }}///<summary>///constructor///</summary> public maxlengthvalidator () {this. Validatefornonnationalfield = true; } protected override bool EvaluateIsValid () {var targetcontrolname = this. ControlToValidate; if (Targetcontrolname = = null) {//There is no target validation control, verify by return true;} var Targetcontrol = this. Namingcontainer.findcontrol (Targetcontrolname) as TextBox; if (Targetcontrol = = null) {//target control is not a text box or does not exist, verifyPass the pass return true; } var maxLength = targetcontrol.maxlength; var targetvalue = this. Getcontrolvalidationvalue (Targetcontrolname); var targetlength = 0; if (string. IsNullOrEmpty (Targetvalue)) {targetlength = 0;} else {if (this. Validatefornonnationalfield) {targetlength = this. Getlengthfornonnationnal (Targetvalue); } else {targetlength = targetvalue.length;}} return targetlength <= maxLength; }///<summary>//For the length of text when saved to varchar///</summary>//<param name= "text" ></param>///< returns></returns> public virtual int getlengthfornonnationnal (string text) {if (string. IsNullOrEmpty (text)) {return 0;} var length = 0; for (var i = 0; i < text. ToCharArray (). Length; i++) {var charcode = (int) text[i]; length++; if (CharCode > 255) {length++;}} return length; } protected override void AddAttributesToRender (HtmlTextWriter writer) {base. AddAttributesToRender (writer); if (this. Renderuplevel) {writer. AddAttribute ("TargetControlID", thiS.getcontrolrenderid (this. ControlToValidate)); Writer. AddAttribute ("Validatefornonnationalfield", this. Validatefornonnationalfield? "True": "false"); Writer. AddAttribute ("Evaluationfunction", "maxlengthvalidator_validate"); var Targetcontrol = this. Namingcontainer.findcontrol (this. ControlToValidate) as a TextBox; if (Targetcontrol! = null) {var maxLength = targetcontrol.maxlength; writer. AddAttribute ("MaxLength", maxlength.tostring ()); }}} protected override void OnPreRender (EventArgs e) {var script = @ "function maxlengthvalidator_getnonnationallength ( Text) {if (!text) {return 0;} var length = 0; for (var i = 0; i < text.length; i++) {var charcode = Text.charcodeat ( i); length++; if (CharCode > 255) {length++;}} return length; } function Maxlengthvalidator_validate (val) {if (!val) {return true;} var Targetid = val. TargetControlID; if (!targetid) {return true;} var nonnational = val. Validatefornonnationalfield; if (!nonnational) {return true;} var target = DocumenT.getelementbyid (Targetid); if (!target) {return true;} var maxLength = val.maxlength; if (!maxlength | | IsNaN (maxLength)) {return true;} var targetvalue = Target.value; if (!targetvalue) {return true;} var reallength = targetvalue.length; if (nonnational = = ' true ') {reallength = Maxlengthvalidator_getnonnationallength (Targetvalue);} return reallength <= parseint (maxLength); }"; Scriptmanager.registerclientscriptblock (This, typeof (Maxlengthvalidator), "Maxlengthvalidator_validate", script, true); Base. OnPreRender (e); } }