Specifying maxlength for multiline textbox

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

I\'m trying to use asp:


I want a way to spe

相关标签:
19条回答
  • 2020-11-27 15:59
    $('#txtInput').attr('maxLength', 100);
    
    0 讨论(0)
  • 2020-11-27 16:03

    Things have changed in HTML5:

    ASPX:

    <asp:TextBox ID="txtBox" runat="server" maxlength="2000" TextMode="MultiLine"></asp:TextBox>
    

    C#:

    if (!IsPostBack)
    {
        txtBox.Attributes.Add("maxlength", txtBox.MaxLength.ToString());
    }
    

    Rendered HTML:

    <textarea name="ctl00$DemoContentPlaceHolder$txtBox" id="txtBox" maxlength="2000"></textarea>
    

    The metadata for Attributes:

    Summary: Gets the collection of arbitrary attributes (for rendering only) that do not correspond to properties on the control.

    Returns: A System.Web.UI.AttributeCollection of name and value pairs.

    0 讨论(0)
  • 2020-11-27 16:04

    The following example in JavaScript/Jquery will do that-

    <telerik:RadScriptBlock ID="RadScriptBlock1" runat="server">
    <script type="text/javascript">
         function count(text, event) {
    
             var keyCode = event.keyCode;
    
             //THIS IS FOR CONTROL KEY
             var ctrlDown = event.ctrlKey;
    
             var maxlength = $("#<%=txtMEDaiSSOWebAddress1.ClientID%>").val().length;
    
             if (maxlength < 200) {
                 event.returnValue = true;
             }
             else {
    
                 if ((keyCode == 8) || (keyCode == 9) || (keyCode == 46) || (keyCode == 33) || (keyCode == 27) || (keyCode == 145) || (keyCode == 19) || (keyCode == 34) || (keyCode == 37) || (keyCode == 39) || (keyCode == 16) || (keyCode == 18) ||
                     (keyCode == 38) || (keyCode == 40) || (keyCode == 35) || (keyCode == 36) || (ctrlDown && keyCode == 88) || (ctrlDown && keyCode == 65) || (ctrlDown && keyCode == 67) || (ctrlDown && keyCode == 86)) 
    
                      {
                     event.returnValue = true;
                      }
    
                 else {
    
                     event.returnValue = false;
                 }
             }
    
         }
    
         function substr(text)
          {
              var txtWebAdd = $("#<%=txtMEDaiSSOWebAddress1.ClientID%>").val();
              var substrWebAdd;
              if (txtWebAdd.length > 200) 
              {                 
                  substrWebAdd = txtWebAdd.substring(0, 200);                                  
                  $("#<%=txtMEDaiSSOWebAddress1.ClientID%>").val('');
                  $("#<%=txtMEDaiSSOWebAddress1.ClientID%>").val(substrWebAdd); 
    
              }
         }                  
    

    0 讨论(0)
  • 2020-11-27 16:07

    I tried different approaches but every one had some weak points (i.e. with cut and paste or browser compatibility). This is the solution I'm using right now:

    function multilineTextBoxKeyUp(textBox, e, maxLength) {
        if (!checkSpecialKeys(e)) {
            var length = parseInt(maxLength);
            if (textBox.value.length > length) {
                textBox.value = textBox.value.substring(0, maxLength);
            }
        }
    }
    
    function multilineTextBoxKeyDown(textBox, e, maxLength) {
        var selectedText = document.selection.createRange().text;
        if (!checkSpecialKeys(e) && !e.ctrlKey && selectedText.length == 0) {
            var length = parseInt(maxLength);
            if (textBox.value.length > length - 1) {
                if (e.preventDefault) {
                    e.preventDefault();
                }
                else {
                    e.returnValue = false;
                }
            }
        }
    }
    
    function checkSpecialKeys(e) {
        if (e.keyCode != 8 && e.keyCode != 9 && e.keyCode != 33 && e.keyCode != 34 && e.keyCode != 35 && e.keyCode != 36 && e.keyCode != 37 && e.keyCode != 38 && e.keyCode != 39 && e.keyCode != 40) {
            return false;
        } else {
            return true;
        }
    }
    

    In this case, I'm calling multilineTextBoxKeyUp on key up and multilineTextBoxKeyDown on key down:

    myTextBox.Attributes.Add("onkeyDown", "multilineTextBoxKeyDown(this, event, '" + maxLength + "');");
    myTextBox.Attributes.Add("onkeyUp", "multilineTextBoxKeyUp(this, event, '" + maxLength + "');");
    
    0 讨论(0)
  • 2020-11-27 16:07

    Here's how we did it (keeps all code in one place):

    <asp:TextBox ID="TextBox1" runat="server" TextMode="MultiLine"/>
    <% TextBox1.Attributes["maxlength"] = "1000"; %>
    

    Just in case someone still using webforms in 2018..

    0 讨论(0)
  • 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" %>
    
    <mp:LimitedMultiLineTextBox runat="server" Rows="3" ...
    
    0 讨论(0)
提交回复
热议问题