Display Textbox count on a Label

This solution will help you use asp Text box and a Label to display the count of characters,
Add below code on your Default.aspx,
<asp:TextBox ID="txtComments" runat="server" TextMode="MultiLine" 
     MaxLength="160" ></asp:TextBox>   
<asp:Label ID="lblCharacters" runat="server"></asp:Label>

Add below javascript code on your default.aspx,
<script language="javascript" type="text/javascript">
    function DisplayCount() 
    {
        var length = document.getElementById('<%=txtComments.ClientID%>').value.length;
        document.getElementById('<%=lblCharacters.ClientID%>').innerText = length;
    }
</script>

Add following code on your Page_load event,
txtComments.Attributes.Add("onkeypress", "javascript:return DisplayCount();");

After adding above code, you can see the count on the label when you type in the textbox.

No comments:

Post a Comment