Specifying maxlength for multiline textbox

后端 未结 19 1840
感动是毒
感动是毒 2020-11-27 15:21

I\'m trying to use asp:


I want a way to spe

相关标签:
19条回答
  • 2020-11-27 16:21

    Nearly all modern browsers now support the use of the maxlength attribute for textarea elements.(https://caniuse.com/#feat=maxlength)

    To include the maxlength attribute on a multiline TextBox, you can simply modify the Attributes collection in the code behind like so:

    txtTextBox.Attributes["maxlength"] = "100";
    

    If you don't want to have to use the code behind to specify that, you can just create a custom control that derives from TextBox:

    public class Textarea : TextBox
    {
        public override TextBoxMode TextMode
        {
            get { return TextBoxMode.MultiLine; }
            set { }
        }
    
        protected override void OnPreRender(EventArgs e)
        {
            if (TextMode == TextBoxMode.MultiLine && MaxLength != 0)
            {
                Attributes["maxlength"] = MaxLength.ToString();
            }
    
            base.OnPreRender(e);
        }
    }
    
    0 讨论(0)
提交回复
热议问题