textbox的textmode取为multiline时,其maxlength不起作用

当ASP.NET中的textbox设置为multiline模式时,其maxlength属性默认不起作用。本文提出了三种解决方案:1) 使用RegularExpressionValidator进行验证;2) 通过弹出对话框提示超出字数;3) 自定义控件实现长度验证。详细步骤和代码示例在原文档中给出。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

实际应用中,遇到了这个问题,有以下三种解决方案:

 

方案一:  验证控件验证(经实践可行)

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"  style ="Z-INDEX: 102; LEFT: 200px; POSITION: absolute; TOP: 104px"  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

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值