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
Solved

Pass parameter from ASP.NET to Javascript

Posted on 2010-09-18
5
500 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 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

Free Tool: Port Scanner

Check which ports are open to the outside world. Helps make sure that your firewall rules are working as intended.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

It was really hard time for me to get the understanding of Delegates in C#. I went through many websites and articles but I found them very clumsy. After going through those sites, I noted down the points in a easy way so here I am sharing that unde…
International Data Corporation (IDC) prognosticates that before the current the year gets over disbursing on IT framework products to be sent in cloud environs will be $37.1B.
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…

839 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