Solved

Totalling dynamically created textboxes on blur

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

Master Your Team's Linux and Cloud Stack

Come see why top tech companies like Mailchimp and Media Temple use Linux Academy to build their employee training programs.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
Jquery datatables integration with ASP.NET MVC, bootstrap 3 22
get combo value in class 5 14
jquery tab header text 1 22
Code works but breaks when I add one section 4 20
ASP.Net to Oracle Connectivity Recently I had to develop an ASP.NET application connecting to an Oracle database.As I am doing it first time ,I had to solve several problems. This article will help to such developers  to develop an ASP.NET client…
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…
This video shows how to quickly and easily add an email signature for all users on Exchange 2016. The resulting signature is applied on a server level by Exchange Online. The email signature template has been downloaded from: www.mail-signatures…

839 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