473,320 Members | 1,902 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,320 software developers and data experts.

How to AutoSum text box values in a separate text box

Greetings,

I'm developing an ASP.NET web page, which includes three text boxes for users to enter customer revenues. I want to code a formula such that the value in the Total Revenue text box (txtRev_Expr_Tot) equals the sum of values entered in the Current Revenue (txtRev_Expr_Curr) and Incremental Revenue (txtRev_Expr_Incr) text boxes. I also want the Total box to update in OnLostFocus events from the other two boxes.

I'm new to ASP.NET development, and am unsure how to code this. Whatever help or suggestions anyone can offer would be greatly appreciated.

J
Oct 16 '09 #1
5 5832
Frinavale
9,735 Expert Mod 8TB
Sounds like this can all be done using JavaScript...?
Oct 16 '09 #2
Should I re-post to the JavaScript forum?
Oct 16 '09 #3
Frinavale
9,735 Expert Mod 8TB
Nope...that's ok.
The reason I suggested using JavaScript is because of the "onblur" event that you want to use....
Have you ever worked with JavaScript before?
Did you want to do this client side or server-side?
Does it matter?
Oct 16 '09 #4
It doesn't matter how I make it work. A colleague had suggested an "onblur" event, but I'm not required to use it. I have some experience with JavaScript, but not much. However I can get the text boxes to sum will work for me. Any suggestions?
Oct 19 '09 #5
Frinavale
9,735 Expert Mod 8TB
I'm not sure if you're using VB.NET or C# for your server code.

But the idea is to configure the TextBox elements to call a JavaScript method during the JavaScript onblur event.

To do this you use the TextBox's Attributes property like so:

VB.NET:
Expand|Select|Wrap|Line Numbers
  1. TextBox2.Attributes.Add("onblur","doSum()")
The C# is pretty much the same thing:
Expand|Select|Wrap|Line Numbers
  1. TextBox2.Attributes.Add("onblur","doSum()");
Now all you have to do is add the "doSum()" JavaScript method to the page somehow. It could be in an external JavaScript file, written directly into the ASP page, or you could dynamically generate the doSum() method in your VB or C# code and register it with the page (using either the ScriptManager if you're using Ajax or the Page.ClientScript if you're not using Ajax).

I would probably recommend writing the method directly into the ASP page because it's easiest.

Now, the important thing to note is that your TextBox ID client-side may not be the same as the TextBox ID server-side. This has to do with the NamingContainer to ensure that every element on the page has a unique ID client side. For example, you could have a TextBox1 in User Control "A" and you could also have a TextBox1 in User Control "B". In order to keep your HTML valid ASP.NET gives these TextBoxes unique IDs when they are rendered int he browser. This means that TextBox1 in User Control A would have an ID client side something like 'ctrl100_UserControlA_TextBox1" and TextBox1 in User Control B would have the ID: 'ctrl100_UserControlB_TextBox1".

To access this unique, client-side ID in your server-side code you use the TextBox's ClientID property.

You could pass these clientID's as parameters to the "doSum()" JavaScript method and then use the document.getElementById() to grab the values in the TextBoxes that need to be added together.

But to do this you'll have to change the TextBox's Attributes property to be:
VB.NET:
Expand|Select|Wrap|Line Numbers
  1. TextBox1.Attributes.Add("onblur","doSum('"+TextBox1.ClientID+"','"+TextBox2.ClientID+"')")
C# :
Expand|Select|Wrap|Line Numbers
  1. TextBox1.Attributes.Add("onblur","doSum('"+TextBox1.ClientID+"','"+TextBox2.ClientID+"')");
If you don't want to pass parameters into the doSum method then you'd use the ASP Response.Write() method to dynamically write the ClientID of the TextBoxes directly into the JavaScript code in your ASP page......

For example, this code would be placed on the ASP page....Please note that anything between the <% %> is executed on the server. The following is VB.NET code:
Expand|Select|Wrap|Line Numbers
  1. function doSum(){
  2.   var textBox1 = document.getElementById('<%Response.Write("TextBox1.ClientID")%>');
  3.   var num1 = Number(textBox1.value);
  4.  
  5.   var textBox2 = document.getElementById('<%Response.Write("TextBox2.ClientID")%>');
  6.   var num2 = Number(textBox2.value);
  7.  
  8.   var sum = num1 + num2;
  9. }
The ASP shorthand for Response.Write is <%= %>....so you could also have:
Expand|Select|Wrap|Line Numbers
  1. function doSum(){
  2.   var textBox1 = document.getElementById('<%=TextBox1.ClientID%>');
  3.   var num1 = Number(textBox1.value);
  4.  
  5.   var textBox2 = document.getElementById('<%=TextBox2.ClientID%>');
  6.   var num2 = Number(textBox2.value);
  7.  
  8.   var sum = num1 + num2;
  9. }
That should get you started :)

-Frinny
Oct 19 '09 #6

Sign in to post your reply or Sign up for a free account.

Similar topics

5
by: Paul C-T | last post by:
Hi, Am I trying to be too clever here? I am trying to write a PHP page to enable me to enter values into a form then write those values to a text file. I want to use the form & table that...
6
by: Suresh Kumaran | last post by:
Hi All, Does anybody know the sytax in VB.NET to write the contents of a multiline text box to a text file? Appreciate help. Suresh
3
by: JK | last post by:
This is an easy one, but I'm a JS newbie. I want to write a function that determines input value. If 0 - 100 than will use input as % (i.e., 3 would become .03 and used for calculations);...
23
by: shank | last post by:
I have the below code found on an ASP site. <% arrName = Split(Request("TextArea"),",") %> <% For i = LBound(arrName) To UBound(arrName) Response.Write "ID: " & arrName(i) & "<br>" Next %>
1
by: hdogg | last post by:
So, this autosum works perfect but... since i'm dyamically going to submit multiple records... all of the form names will be the same ..." forecast" but when i try to sum all the fields that the...
5
by: rn5a | last post by:
Can someone please suggest me a text editor especially for DEBUGGING ASP scripts apart from Microsoft Visual Interdev? I tried using Visual Interdev & created a project but Interdev generates...
0
by: JosAH | last post by:
Greetings, Introduction Last week I started thinking about a text processing facility. I already found a substantial amount of text: a King James version of the bible. I'm going to use that...
2
by: sammiesue | last post by:
Hi, I have form with 2 autosummed textboxes ("total" and "casinototal"). I would like to have a grand total textbox ("grandtotal") get its value from summing "total" and "casinototal", but it...
3
by: davncong | last post by:
I've created a webform for a user to order from a catalog. There are five colums (item #, quantity, description, price and total) and 10 rows (repeating first row so the user can order up to 10...
1
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
0
by: Vimpel783 | last post by:
Hello! Guys, I found this code on the Internet, but I need to modify it a little. It works well, the problem is this: Data is sent from only one cell, in this case B5, but it is necessary that data...
0
by: jfyes | last post by:
As a hardware engineer, after seeing that CEIWEI recently released a new tool for Modbus RTU Over TCP/UDP filtering and monitoring, I actively went to its official website to take a look. It turned...
0
by: ArrayDB | last post by:
The error message I've encountered is; ERROR:root:Error generating model response: exception: access violation writing 0x0000000000005140, which seems to be indicative of an access violation...
1
by: PapaRatzi | last post by:
Hello, I am teaching myself MS Access forms design and Visual Basic. I've created a table to capture a list of Top 30 singles and forms to capture new entries. The final step is a form (unbound)...
0
by: Defcon1945 | last post by:
I'm trying to learn Python using Pycharm but import shutil doesn't work
1
by: Shællîpôpï 09 | last post by:
If u are using a keypad phone, how do u turn on JavaScript, to access features like WhatsApp, Facebook, Instagram....
0
by: af34tf | last post by:
Hi Guys, I have a domain whose name is BytesLimited.com, and I want to sell it. Does anyone know about platforms that allow me to list my domain in auction for free. Thank you
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.