Search This Blog

Tuesday, September 6, 2011

TextBox with TextMode="MultiLine" is not woking in asp.net

By default TextBox won't support Maxlength when we set TextMode="MultiLine".
To support maxlength we have to call javascript function to restrict the number of characters.

Below example solve the above problem :

<script type="text/javascript">

 function AdditionalInfoRestriction() {
     var txtAddInfo = document.getElementById('<%=txtAdditionalInformation.ClientID %>');
                    try {
                         // 255 is maxlength of the textbox
                        if (txtAddInfo.value.length > 255) {
                            var cont = txtAddInfo.value;
                            txtAddInfo.value = cont.substring(0, 254);
                            return false;
                        }
                    }
                    catch (e) {
                    }
                }
 </script>

<asp:TextBox CssClass="txt" ID="txtAdditionalInformation"  
        onkeyup="return AdditionalInfoRestriction();"
        Width="440px" TextMode="MultiLine" MaxLength="250"  runat="server">
</asp:TextBox>
Post a Comment

Popular Posts