方法一: 驗證控件驗證(經實踐可行)
Setting the Maxength of a TextBox when it is in Multiline, You can use RegularExpressionValidator control as shown below
<ASP:TextBox ID="txtConclusion" MaxLength="200" TextMode="MultiLine" Height="100px" Width="400px" runat="server" />
<ASP:RegularExpressionValidator ID="txtConclusionValidator1" ControlToValidate="txtConclusion" Text="超過200字" ValidationExpression="^[\s\S]{0,200}$" runat="server" />
方法二:彈出對話框提示
1.Html代碼
<Html>
<HEAD>
<title>WebForm6</title>
<meta name="GENERATOR" Content="Microsoft Visual Studio .Net 7.1">
<meta name="CODE_LANGUAGE" Content="C#">
<meta name="vs_defaultClIEntScript" content="JavaScript">
<meta name="vs_targetSchema" content="http://schemas.microsoft.com/intellisense/IE5">
<script language="Javascript">
function isOver(sText,len)
{
var intlen=sText.value.length;
if (intlen>len)
{
alert("The content length must Less than or Equal "+len);
sText.focus();
sText.select();
}
}
</script>
</HEAD>
<body MS_POSITIONING="GridLayout">
<form id="Form1" method="post" runat="server">
<ASP:TextBox id="txtName" runat="server"
TextMode="MultiLine" Height="112px" Width="271px"></ASP:TextBox>
</form>
</body>
</Html>2.cs代碼
private void Page_Load(object sender, System.EventArgs e)
{
this.txtName.Attributes.Add("onblur","isOver(this,1000);");
}
方法三: 自定義控件
Multiline TextBox with MaxLength Validation
http://www.codeproject.com/KB/aspnet/Textarea_Length_Validator.ASPx