Still celebrating National IT Professionals Day with 3 months of free Premium Membership. Use Code ITDAY17

x
?
Solved

Totalling dynamically created textboxes on blur

Posted on 2007-11-29
1
Medium Priority
?
1,054 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 2000 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

[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

IntroductionWhile developing web applications, a single page might contain many regions and each region might contain many number of controls with the capability to perform  postback. Many times you might need to perform some action on an ASP.NET po…
User art_snob (http://www.experts-exchange.com/M_6114203.html) encountered strange behavior of Android Web browser on his Mobile Web site. It took a while to find the true cause. It happens so, that the Android Web browser (at least up to OS ver. 2.…
This course is ideal for IT System Administrators working with VMware vSphere and its associated products in their company infrastructure. This course teaches you how to install and maintain this virtualization technology to store data, prevent vuln…
Add bar graphs to Access queries using Unicode block characters. Graphs appear on every record in the color you want. Give life to numbers. Hopes this gives you ideas on visualizing your data in new ways ~ Create a calculated field in a query: …

660 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