Solved

Pass parameter from ASP.NET to Javascript

Posted on 2010-09-18
5
487 Views
Last Modified: 2012-05-10
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

0
Comment
Question by:pstanford
5 Comments
 
LVL 82

Accepted Solution

by:
Dave Baldwin earned 250 total points
ID: 33710564
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
 
LVL 12

Expert Comment

by:Mohamed Abowarda
ID: 33710625
0
 
LVL 12

Expert Comment

by:jagssidurala
ID: 33710626
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
 

Author Closing Comment

by:pstanford
ID: 33710856
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
 
LVL 82

Expert Comment

by:Dave Baldwin
ID: 33711993
Thanks and I think that's a better way anyway.
0

Featured Post

What Security Threats Are You Missing?

Enhance your security with threat intelligence from the web. Get trending threat insights on hackers, exploits, and suspicious IP addresses delivered to your inbox with our free Cyber Daily.

Join & Write a Comment

Introduction HTML checkboxes provide the perfect way for a web developer to receive client input when the client's options might be none, one or many.  But the PHP code for processing the checkboxes can be confusing at first.  What if a checkbox is…
This article will give core knowledge of JavaScript and will head in to your first JavaScript program. I am Durvesh Naik and I am here to deal with this series of JavaScript. I will teach you JavaScript in part wise , as its quite boring to read big…
The viewer will learn the basics of jQuery, including how to invoke it on a web page. Reference your jQuery libraries: (CODE) Include your new external js/jQuery file: (CODE) Write your first lines of code to setup your site for jQuery.: (CODE)
The viewer will learn the basics of jQuery including how to code hide show and toggles. Reference your jQuery libraries: (CODE) Include your new external js/jQuery file: (CODE) Write your first lines of code to setup your site for jQuery…

747 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

13 Experts available now in Live!

Get 1:1 Help Now