TextBox.MaxLength 屬性獲取或設置文本框中最多允許的字符數文本框中最多允許的字符數。默認值為 0,表示未設置該屬性。使用 MaxLength 屬性限定可以在 TextBox 控件中輸入的字符數。
注意 :僅當 TextMode 屬性設置為 TextBoxMode.SingleLine 或 TextBoxMode.PassWord 時,此屬性才適用。
關于TextBox的MaxLength無效問題
方法一: 驗證控件(經實踐可行) 驗證控件
代碼如下 復制代碼 <asp:TextBox ID="txtConclusion" MaxLength="200" TextMode="MultiLine" Height="100px" Width="400px" runat="server" /><asp:RegularExPRessionValidator ID="txtConclusionValidator1" ControlToValidate="txtConclusion" Text="超過200字" ValidationExpression="^[sS]{0,200}$" runat="server" />
方法二:添加一些客戶端限制的JS代碼。示例如下:代碼
代碼如下 復制代碼 <HTML> <HEAD> <title>WebForm6</title> <meta name="GENERATOR" Content="Microsoft Visual Studio .NET 7.1"> <meta name="CODE_LANGUAGE" Content="C#"> <meta name="vs_defaultClientScrjavaScript"> <meta name="vs_targetSchema" content="http://schemas.microsoft.c(www.111cn.net)om/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" style="Z-INDEX: 102; LEFT: 200px; POSITION: absolute; TOP: 104px" runat="server" TextMode="MultiLine" Height="112px" Width="271px"></asp:TextBox> </form> </body></HTML>Public void Page_Load(object sender, System.EventArgs e){ this.txtName.Attributes.Add("onblur","isOver(this,1000);");} from:http://www.111cn.net/net/160/66645.htm
新聞熱點
疑難解答