项目会遇到标题所示的原因,今天趁空闲时间大概找了一下原因,借此抛砖引玉,希望大家提出更好的方法。
Asp.net TextBox控件TextBoxMode设置为MultiLine,maxlength属性失效是正常的,当TextBoxMode设置为 MultiLine时,生成的前台控件是textArea,而它是没有maxlength的属性的,而<input type="text">则有此属性。从TextBox的源码我们也可以看出一斑。
所以我们必须自己去做这部分工作,其实也很简单,加几个属性就好了,这里需要注意,用键盘输入和鼠标右键粘贴的情况。最简单的做法我是写一个自定义控件,继承于TextBox。
大概代码如下。
public class MulTextBox :TextBox
{
protected override void OnPreRender(EventArgs e)
{
if (this.Page != null)
{
if (this.TextMode == TextBoxMode.MultiLine)
{
this.Page.ClientScript.RegisterClientScriptResource(typeof(MulTextBox), "MulTextBox.CheckFunction.js");
this.Attributes.Add("onmousedown", "CheckTextboxLength('" + this.ClientID + "','" + this.MaxLength + "')");
this.Attributes.Add("onkeyup", "CheckTextboxLength('" + this.ClientID + "','" + this.MaxLength + "')");
this.Attributes.Add("onmouseout", "CheckTextboxLength('" + this.ClientID + "','" + this.MaxLength + "')");
}
}
base.OnPreRender(e);
}
}
javascript:
function CheckTextboxLength(obj,maxlength)
{
var textObj = document.getElementById(obj);
var len = textObj.value.length;
if(len > maxlength)
{
textObj.value = textObj.value.substring(0,maxlength);
}
}
这里没有使用window.clipboardData.getData("Text")是由于使用此脚本会弹出一个提示,客户体验不是很好。
Asp.net TextBox控件TextBoxMode设置为MultiLine,maxlength属性失效是正常的,当TextBoxMode设置为 MultiLine时,生成的前台控件是textArea,而它是没有maxlength的属性的,而<input type="text">则有此属性。从TextBox的源码我们也可以看出一斑。
protected override void AddAttributesToRender(HtmlTextWriter writer)注意标黄的部分这里但是multiline的时候是没有做maxlength的控制的。
{
int maxLength;
if (this.Page != null)
{
this.Page.VerifyRenderingInServerForm(this);
}
writer.AddAttribute(HtmlTextWriterAttribute.Name, this.UniqueID);
switch (this.TextMode)
{
case TextBoxMode.SingleLine:
{
writer.AddAttribute(HtmlTextWriterAttribute.Type, "text");
string text = this.Text;
if (text.Length > 0)
{
writer.AddAttribute(HtmlTextWriterAttribute.Value, text);
}
break;
}
case TextBoxMode.Password:
writer.AddAttribute(HtmlTextWriterAttribute.Type, "password");
break;
case TextBoxMode.MultiLine:
maxLength = this.Rows;
if (maxLength > 0)
{
writer.AddAttribute(HtmlTextWriterAttribute.Rows, maxLength.ToString(NumberFormatInfo.InvariantInfo));
}
maxLength = this.Columns;
if (maxLength > 0)
{
writer.AddAttribute(HtmlTextWriterAttribute.Cols, maxLength.ToString(NumberFormatInfo.InvariantInfo));
}
if (!this.Wrap)
{
writer.AddAttribute(HtmlTextWriterAttribute.Wrap, "off");
}
goto Label_00FF;
}
maxLength = this.MaxLength;
if (maxLength > 0)
{
writer.AddAttribute(HtmlTextWriterAttribute.Maxlength, maxLength.ToString(NumberFormatInfo.InvariantInfo));
}
maxLength = this.Columns;
if (maxLength > 0)
{
writer.AddAttribute(HtmlTextWriterAttribute.Size, maxLength.ToString(NumberFormatInfo.InvariantInfo));
}
Label_00FF:
if (this.ReadOnly)
{
writer.AddAttribute(HtmlTextWriterAttribute.ReadOnly, "readonly");
}
if (this.AutoPostBack && (this.Page != null))
{
string postBackClientEvent = this.Page.GetPostBackClientEvent(this, "");
if (base.HasAttributes)
{
string str3 = base.Attributes["onchange"];
if (str3 != null)
{
postBackClientEvent = str3 + postBackClientEvent;
base.Attributes.Remove("onchange");
}
}
writer.AddAttribute(HtmlTextWriterAttribute.Onchange, postBackClientEvent);
writer.AddAttribute("language", "javascript");
}
base.AddAttributesToRender(writer);
}
所以我们必须自己去做这部分工作,其实也很简单,加几个属性就好了,这里需要注意,用键盘输入和鼠标右键粘贴的情况。最简单的做法我是写一个自定义控件,继承于TextBox。
大概代码如下。
public class MulTextBox :TextBox
{
protected override void OnPreRender(EventArgs e)
{
if (this.Page != null)
{
if (this.TextMode == TextBoxMode.MultiLine)
{
this.Page.ClientScript.RegisterClientScriptResource(typeof(MulTextBox), "MulTextBox.CheckFunction.js");
this.Attributes.Add("onmousedown", "CheckTextboxLength('" + this.ClientID + "','" + this.MaxLength + "')");
this.Attributes.Add("onkeyup", "CheckTextboxLength('" + this.ClientID + "','" + this.MaxLength + "')");
this.Attributes.Add("onmouseout", "CheckTextboxLength('" + this.ClientID + "','" + this.MaxLength + "')");
}
}
base.OnPreRender(e);
}
}
javascript:
function CheckTextboxLength(obj,maxlength)
{
var textObj = document.getElementById(obj);
var len = textObj.value.length;
if(len > maxlength)
{
textObj.value = textObj.value.substring(0,maxlength);
}
}
这里没有使用window.clipboardData.getData("Text")是由于使用此脚本会弹出一个提示,客户体验不是很好。

浙公网安备 33010602011771号