Connecting Tech Pros Worldwide Help | Site Map

How to AutoSum text box values in a separate text box

Member
 
Join Date: Feb 2008
Posts: 80
#1: Oct 16 '09
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
Frinavale's Avatar
Site Moderator
 
Join Date: Oct 2006
Location: The Great White North
Posts: 5,066
#2: Oct 16 '09

re: How to AutoSum text box values in a separate text box


Sounds like this can all be done using JavaScript...?
Member
 
Join Date: Feb 2008
Posts: 80
#3: Oct 16 '09

re: How to AutoSum text box values in a separate text box


Should I re-post to the JavaScript forum?
Frinavale's Avatar
Site Moderator
 
Join Date: Oct 2006
Location: The Great White North
Posts: 5,066
#4: Oct 16 '09

re: How to AutoSum text box values in a separate text box


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?
Member
 
Join Date: Feb 2008
Posts: 80
#5: 4 Weeks Ago

re: How to AutoSum text box values in a separate text box


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?
Frinavale's Avatar
Site Moderator
 
Join Date: Oct 2006
Location: The Great White North
Posts: 5,066
#6: 4 Weeks Ago

re: How to AutoSum text box values in a separate text box


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
Reply