Solved

Pass parameter from ASP.NET to Javascript

Posted on 2010-09-18
5
504 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
5 Comments
 
LVL 83

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 83

Expert Comment

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

Featured Post

Get 15 Days FREE Full-Featured Trial

Benefit from a mission critical IT monitoring with Monitis Premium or get it FREE for your entry level monitoring needs.
-Over 200,000 users
-More than 300,000 websites monitored
-Used in 197 countries
-Recommended by 98% of users

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Calculating holidays and working days is a function that is often needed yet it is not one found within the Framework. This article presents one approach to building a working-day calculator for use in .NET.
This article demonstrates how to create a simple responsive confirmation dialog with Ok and Cancel buttons using HTML, CSS, jQuery and Promises
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…
Suggested Courses

628 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