Mona Coder Mona Coder - 5 months ago 10
Javascript Question

Issue on Counting Text Box Left Character length in APT.NET and jQuery

I have a multi-line textbox in ASP.Net application which looks like this:

<asp:TextBox
ID="NeedDescriptionText"
runat="server"
TextMode="multiline"
Rows="5"
Width="78%"
MaxLength="500"
ValidationGroup="DescriptionText">
</asp:TextBox>


and a span to display the result of left character as:

<span id="NeedDescriptionLimit"></span><span> Character left</span>


Now I have jQuery code which is working perfectly on this DEMO as it is like :

<script type="text/javascript">
$(document).ready(function () {
// Text Box limit count.
var origCnt = 8;
alert("This is a test");
$('#NeedDescriptionLimit').html(origCnt);

$('#NeedDescriptionText').keyup(function (e) {
var currentlen = $('#NeedDescriptionText').val().length;
if (currentlen <= origCnt) {
$('#NeedDescriptionLimit').html(origCnt - currentlen);
} else {
$(this).val($(this).val().substring(0, origCnt));
}
});

});
</script>


but it is not functioning on ASP.NET app. I put the
alert()
in the code to test if jQuery is working which it was and I am getting the alert on opening the page but the character counting not working at all.

Can you please let me know why this is happening?

Thanks

Answer

one option:

add ClientIDMode="static" to the textbox

or replace this:

$('#NeedDescriptionText')

with this

$("<%= NeedDescriptionText.ClientID %>")

ClientIDMode

The ClientID value is generated by concatenating the ID values of each parent naming container with the ID value of the control. In data-binding scenarios where multiple instances of a control are rendered, an incrementing value is inserted in front of the control's ID value. Each segment is separated by an underscore character (_).

Comments