我们再用TextBox时有一个MaxLength属性很管用,可以控制文本长度。但是当文本可以换行时,MaxLength就不起作用了,而且它把一个汉字也当作一个字符,这样如果数据库是varchar的话MaxLength就只能设数据库值得一半,造成浪费。 所以通过Google我找到了一种把通过客户端脚本和CustomValidator验证控件配合来验证TextBox长度的方法。 < script type = " text/javascript " > <!-- String.prototype.len = function () { return this.replace(/[^\x00-\xff]/g,"**").length; } function CheckLength(source, arguments) { var temp; temp=source.controltovalidate ; if (arguments.Value.len()<= eval("document.all." + temp).maxlength) arguments.IsValid = true; else arguments.IsValid = false; } // --> </ script > 下面是Visual Studio 2005 的代码下载。 /Files/wuqing17/MultiLineTextBox.rar 转载于:https://www.cnblogs.com/wuqing17/archive/2006/08/29/489214.html