Solved

Totalling dynamically created textboxes on blur

Posted on 2007-11-29
1
1,049 Views
Last Modified: 2013-11-07
I have an asp.net page coded in vb.net that has dynamically created textboxes that hold amounts.
The textboxes are created when a user inputs how many boxes they need and clicks a "create boxes" button.  The boxes appear, the user then enters values into the boxes.  I currently have another button that says "total" and when clicked totals all the boxes.  But is there a way to create a javascript function that will total the boxes automatically on the blur event of the amount textbox? I know I can add the event "onBlur" when the textbox is created, but where I am getting stuck is the creating of the total function.  
Issue: a postback has to occur for the program to see the dyanamically created textboxes, for them to be accessed by a javascript function - correct?  Therefore, if a postback is already occurring, should I not just do the totaling in the postback, after the textboxes are recreated?  
0
Comment
Question by:JaCrews
[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
1 Comment
 
LVL 24

Accepted Solution

by:
Jeff Certain earned 500 total points
ID: 20374199
No postback is required -- the javascript runs completely on the client.

Part of the challenge comes in being able to iterate through the controls on the documents and identify which ones to total. In the past, I've done this using a naming convention.

The code snippet below is somewhat simple -- it assumes that all text boxes have numerical input, will add empty text boxes, only supports 50 text boxes, assumes that the text boxes are numbered sequentially and contiguously from 0, etc.
<script language="javascript" type="text/javascript">
		
			function GetTotal()
			{
				var total = 0;
			  for(i = 0; i < 50; i++)
			  {
			    var ctl = document.getElementById('controlPrefix' + i);
			    if (ctl==null)
						return total;
			    else
			    {
			      total = total + ctl.value;
			    }
			  }
			  
			  return total;
			}
	</script>

Open in new window

0

Featured Post

Resolve Critical IT Incidents Fast

If your data, services or processes become compromised, your organization can suffer damage in just minutes and how fast you communicate during a major IT incident is everything. Learn how to immediately identify incidents & best practices to resolve them quickly and effectively.

Question has a verified solution.

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

The ECB site provides FX rates for major currencies since its inception in 1999 in the form of an XML feed. The files have the following format (reducted for brevity) (CODE) There are three files available HERE (http://www.ecb.europa.eu/stats/exch…
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.
In this video, viewers are given an introduction to using the Windows 10 Snipping Tool, how to quickly locate it when it's needed and also how make it always available with a single click of a mouse button, by pinning it to the Desktop Task Bar. Int…
In this video we outline the Physical Segments view of NetCrunch network monitor. By following this brief how-to video, you will be able to learn how NetCrunch visualizes your network, how granular is the information collected, as well as where to f…

734 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