Go Premium for a chance to win a PS4. Enter to Win

x
?
Solved

Totalling dynamically created textboxes on blur

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

Windows Server 2016: All you need to know

Learn about Hyper-V features that increase functionality and usability of Microsoft Windows Server 2016. Also, throughout this eBook, you’ll find some basic PowerShell examples that will help you leverage the scripts in your environments!

Question has a verified solution.

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

More often than not, we developers are confronted with a need: a need to make some kind of magic happen via code. Whether it is for a client, for the boss, or for our own personal projects, the need must be satisfied. Most of the time, the Framework…
A long time ago (May 2011), I have written an article showing you how to create a DLL using Visual Studio 2005 to be hosted in SQL Server 2005. That was valid at that time and it is still valid if you are still using these versions. You can still re…
this video summaries big data hadoop online training demo (http://onlineitguru.com/big-data-hadoop-online-training-placement.html) , and covers basics in big data hadoop .
Despite its rising prevalence in the business world, "the cloud" is still misunderstood. Some companies still believe common misconceptions about lack of security in cloud solutions and many misuses of cloud storage options still occur every day. …

886 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