Pass parameter from ASP.NET to Javascript

I have an ASP.NET 1.1 application that calls a Javascript function to count the number of characters entered into a textbox. This function works correctly however I now have a need to change one of the parameters in that Javascript function dynamically. The first piece of code is the Javascript function. The second piece of code is the call to that function from code behind.

In the second piece of code there is a value of 250 which is the field length passed to the function as parameter maxlimit. Contextually this will change based on the type of information being entered. I get the corresponding length through a stored procedure that looks at the syscolumns table and I want to substitute this value for the physical 250 embedded in the code behind.

All of the above works to my satisfaction but in essence I want to pass the txtName.MaxLength property to the Javascript function. I've tried replacing the 250 with txtName.MaxLength and the result is NaN, if I use txtName.MaxLength.ToString I get nothing.

I've debugged this and know that the .MaxLength property contains the right value but I'm missing something in the way that I pass this to the Javascript funtion. Any assistance would b greatly appreciated.
<script type="text/javascript">
function textCounter (field, countfield, maxlimit)
{
	if (field.value.length > maxlimit)
		field.value = field.value.substring(0, maxlimit);
	else
		countfield.value = maxlimit - field.value.length;
}
</script>


<TD><asp:textbox onkeydown="textCounter(txtName, this.form.countReferenceLength, 250);" id="txtName" 	onkeyup="textCounter(txtName,this.form.countReferenceLength, 250);" runat="server" Width="100%"				CssClass="DefaultTextBoxStyle"></asp:textbox></TD>

Open in new window

pstanfordDirectorAsked:
Who is Participating?
 
Dave BaldwinConnect With a Mentor Fixer of ProblemsCommented:
Did you try <%=txtName.MaxLength.ToString%>?  You have to write a text value into the javascript because javascript is text.  Also, it won't interpret the ASP.NET values by itself.  You can always look at the "View Source" to see what your code is writing into the javascript.
0
 
jagssiduralaCommented:
place a hidden filed in source file, Assign length from syscolumns in page_load or textbox maxlength in page_load.
then you can access this hidden filed value in javascript.

maxlimit = document.getElementById("HiddenFiledId").value;

 this will give you maxlength in your script.
0
 
pstanfordDirectorAuthor Commented:
I had to figure out where to put it but I changed the Javascript function as shown below and this now does what I need it to do. Thanks and best regards.

            <script type="text/javascript">
            function textCounter (field, countfield)
            {
                  var maxLength = <%=txtName.MaxLength.ToString%>;
                  if (null==maxLength)
                        maxLength = 50;
                  
                  if (field.value.length > maxLength)
                        field.value = field.value.substring(0, maxLength);
                  else
                        countfield.value = maxLength - field.value.length;
            }
            </script>
0
 
Dave BaldwinFixer of ProblemsCommented:
Thanks and I think that's a better way anyway.
0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.