Specifying maxlength for multiline textbox

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

I\'m trying to use asp:


I want a way to spe

19条回答
  •  醉话见心
    2020-11-27 16:07

    This is the same as @KeithK's answer, but with a few more details. First, create a new control based on TextBox.

    using System.Web.UI;
    using System.Web.UI.WebControls;
    
    namespace MyProject
    {
        public class LimitedMultiLineTextBox : System.Web.UI.WebControls.TextBox
        {
            protected override void Render(HtmlTextWriter writer)
            {
                this.TextMode = TextBoxMode.MultiLine;
    
                if (this.MaxLength > 0)
                {
                    writer.AddAttribute(HtmlTextWriterAttribute.Maxlength, this.MaxLength.ToString());
                }
    
                base.Render(writer);
            }
        }
    }  
    

    Note that the code above always sets the textmode to multiline.

    In order to use this, you need to register it on the aspx page. This is required because you'll need to reference it using the TagPrefix, otherwise compilation will complain about custom generic controls.

    <%@ Register Assembly="MyProject" Namespace="MyProject" TagPrefix="mp" %>
    
    

提交回复
热议问题