?
Solved

Javascript to sum fields in gridview not working

Posted on 2008-10-29
2
Medium Priority
?
1,285 Views
Last Modified: 2012-05-05
I have some javascript functions that run on the onkeyup event of some textboxs in a gridview.   Now I'm trying to add an additional function to sum some more.  I'm getting an error on page at the bottom of the browser when the event fires.  The error lists at the line before my new function and says "Object does not support this property or method".

My code is shown below.  The new function I'm adding in SumItUpB.  The other functions still work good (Thanks to some previous help from Experts-Exchange).
The javascript code
 
 function SumItUp(textResult)
			{
				var x = 0;
				var y = 0;
				var z = 0;
				
				var myArray = new Array();
				//add array items, this calls the public server variable we build in rowdatabound
				<%= JSArray %>
				//sum it:
				for (x = 0; x < myArray.length; x++)
				{
				    //multiply the value times one to trick js into always thinking the variable is a number
				    y = y + (myArray[x] * 1.00);
				}
				//display the result
				z = (y * 1);
			    document.getElementById(textResult).value = z.toFixed(2);
				//document.getElementById(textResult).value = (y * 1.00);
				
			    }
			
			function SumItUpB(textResult)
			{
	         var x = 0;
			 var y = 0;
			  
			  var myArray2 = new Array();
			  <%= JSArray2 %>
			  for (x = 0; x < myArray2.length; x++);
			  {
			        y = y + (myArray2[x] * 1.00);
			  }
			  
			  document.getElementById(textResult).value = (y * 1);
			  
			}
         		
			function Multiply(text1, text2, textResult)
			{
				var x = document.getElementById(text1).value;
				var y = document.getElementById(text2).value;
				if(IsNumeric(x) == true && IsNumeric(y) == true)
				{
				document.getElementById(textResult).value = (x * 1) * (y * 1.00);
				}
				else
				{
				document.getElementById(textResult).value = '';
				}
			}
			
					
			function IsNumeric(sText)
            {
               var ValidChars = "0123456789.-";
               var IsNumber=true;
               var Char;
               for (i = 0; i < sText.length && IsNumber == true; i++) 
                  { 
                  Char = sText.charAt(i); 
                  if (ValidChars.indexOf(Char) == -1) 
                     {
                     IsNumber = false;
                     }
                  }
               return IsNumber;
               }
The C# Code:
 
 protected void gvMealsForSell_RowDataBound(object sender, GridViewRowEventArgs e)
    {
 
        if (e.Row.RowType == DataControlRowType.DataRow)
        {
            // Get values for calculation
            TextBox txtQuantity = (TextBox)e.Row.FindControl("txtNumberToBuy"); // quantity
            HiddenField hfMealPrice = (HiddenField)e.Row.FindControl("hfMealPrice"); // meal price
            HiddenField hfTotal = (HiddenField)e.Row.FindControl("hfTotal"); //total 
            
            txtQuantity.Text = "0"; // Default value
 
            // Add javascript to text box with calcualtion values
            txtQuantity.Attributes.Add("onkeyup", "javascript:Multiply('" + txtQuantity.ClientID + "','" + hfMealPrice.ClientID + "','" + hfTotal.ClientID + "');SumItUp('" + this.txtTotalDue.ClientID + "');SumItUpB('" + this.txtTotalItems.ClientID + "')");
       
            //Set up javascript array
            JSArray  += "myArray[" + counter.ToString() + "] = document.getElementById('" + hfTotal.ClientID + "').value;" + Environment.NewLine;
            JSArray2 += "myArray2[" + counter.ToString() + "] = document.getElemenetById('" + txtQuantity.ClientID + "').text;" + Environment.NewLine;
  
            //counter to maintain the js array index
            counter += 1; 
 
        }

Open in new window

0
Comment
Question by:weimha
[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
2 Comments
 
LVL 29

Assisted Solution

by:Pravin Asar
Pravin Asar earned 2000 total points
ID: 22845279
To do math operations on the variable values, it is a good idea to parse as Float

e.g.

var x= 1.0;
var y = "2.5";

var sum = parseFloat(x) + parseFloat(y);
alert (sum);


So where you are trying to add/multiply the variable, please use parseFloat

0
 

Accepted Solution

by:
weimha earned 0 total points
ID: 23008723
I've decided not to do this.
Thanks for the input.
0

Featured Post

[Webinar] Lessons on Recovering from Petya

Skyport is working hard to help customers recover from recent attacks, like the Petya worm. This work has brought to light some important lessons. New malware attacks like this can take down your entire environment. Learn from others mistakes on how to prevent Petya like worms.

Question has a verified solution.

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

Performance in games development is paramount: every microsecond counts to be able to do everything in less than 33ms (aiming at 16ms). C# foreach statement is one of the worst performance killers, and here I explain why.
This article discusses how to implement server side field validation and display customized error messages to the client.
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

764 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