Solved

Totalling dynamically created textboxes on blur

Posted on 2007-11-29
1
1,041 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
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

What Security Threats Are You Missing?

Enhance your security with threat intelligence from the web. Get trending threat insights on hackers, exploits, and suspicious IP addresses delivered to your inbox with our free Cyber Daily.

Join & Write a Comment

Today I had a very interesting conundrum that had to get solved quickly. Needless to say, it wasn't resolved quickly because when we needed it we were very rushed, but as soon as the conference call was over and I took a step back I saw the correct …
If you need to start windows update installation remotely or as a scheduled task you will find this very helpful.
Excel styles will make formatting consistent and let you apply and change formatting faster. In this tutorial, you'll learn how to use Excel's built-in styles, how to modify styles, and how to create your own. You'll also learn how to use your custo…
This video demonstrates how to create an example email signature rule for a department in a company using CodeTwo Exchange Rules. The signature will be inserted beneath users' latest emails in conversations and will be displayed in users' Sent Items…

743 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

Need Help in Real-Time?

Connect with top rated Experts

10 Experts available now in Live!

Get 1:1 Help Now