?
Solved

Asp.net Textbox onChange

Posted on 2010-08-30
9
Medium Priority
?
813 Views
Last Modified: 2012-05-10
Hello, I have the following controls for calculating total:
txtSubtotal, txtMisc, txtShipping, lblTotal

I need help figuring out how to do this scenario:
The textbox and label are originally populated from a server side method. I need this to happen if the user changes the text in any of the textboxes.

A new new total is calculated and the lblTotal is changed to the new total. I don't want to post back to the server.

How can I do this?
0
Comment
Question by:gogetsome
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
9 Comments
 
LVL 40

Expert Comment

by:Kyle Abrahams
ID: 33560586
You can use javascript . . . also AJAX might take care of the postback issue for you as well.

0
 
LVL 2

Expert Comment

by:Fahad Pathan
ID: 33560962
@ged325..
AJAX takes d process to server side.. but it takes only partial data.

@gogetsome..
u ve to use javascript or jquery for the same which are client side scripting...
so basically u ve to use client side scripting...
0
 
LVL 11

Expert Comment

by:princeatapi
ID: 33561233
Very well you can go with javascript , thats pretty simple instead of making a post back to perform this simple operation .
0
RHCE - Red Hat OpenStack Prep Course

This course will provide in-depth training so that students who currently hold the EX200 & EX210 certifications can sit for the EX310 exam. Students will learn how to deploy & manage a full Red Hat environment with Ceph block storage, & integrate Ceph into other OpenStack service

 
LVL 8

Accepted Solution

by:
Mohit Vijay earned 2000 total points
ID: 33561322
Create below javascript function
---------------------------------------------------------------
function CalculateTotal()
{
var Sum;
Sum=0;

if (document.getElementById('txtSubtotal').value !="")
Sum = Sum  + Number(document.getElementById('txtSubtotal').value);

if (document.getElementById('txtSubtotal').value !="")
Sum = Sum  + Number(document.getElementById('txtSubtotal').value);

if (document.getElementById('txtSubtotal').value !="")
Sum = Sum  + Number(document.getElementById('txtSubtotal').value);

document.getElementById('lblTotal').innerHTML = Sum;
return false;
}
---------------------------------------------------------------

add attribute with all three textbox

onchange="javascript:return CalculateTotal();" [in you want to calculate on value change]

or

onblur ="javascript:return CalculateTotal();" [in you want to calculate on lost focus]

-------------

Please dont try to copy paste above code, I may have wrtting some wrong syntext, javascript is case sensitve, so please think twice and use above code, Functionality wise my logic is perfect.
0
 

Author Comment

by:gogetsome
ID: 33561919
Thanks Guys! Yes... I want to use some form of clientside javascript.

ViSoft, I put the code below in my application and nothing happens. The casing look correct to me with my limited javascript exposure.. What could be wrong?



I put this into the head of the masterpage:

<script type="text/javascript">

function CalculateTotal()
{
var Sum;
Sum=0;

if (document.getElementById('txtSubtotal').value !="")
Sum = Sum  + Number(document.getElementById('txtSubtotal').value);

if (document.getElementById('txtTax').value !="")
Sum = Sum  + Number(document.getElementById('txtTax').value);

if (document.getElementById('txtMisc').value !="")
Sum = Sum  + Number(document.getElementById('txtMisc').value);

if (document.getElementById('txtShipping').value !="")
Sum = Sum  + Number(document.getElementById('txtShipping').value);

document.getElementById('lblTotal').innerHTML = Sum;
return false;
}

</script>


I put this in each textbox control:

 onchange="javascript:return CalculateTotal();"
0
 

Author Comment

by:gogetsome
ID: 33562019
Additional info which may change things. The controls are within a usercontrol that I place on the page that loads into the masterpage with the javascript. Also, when the usercontrol is first loaded the text boxes are set to false. It is not untill products and shipping are added that the textboxes are set to visible = true...
0
 

Author Closing Comment

by:gogetsome
ID: 33563165
For anyone coming along later. To get the javascript to work you have to have the correct getElementById for each control. To get this just view source of the page when running as the name of the control that the javascript can see is based on where the control resides.

For example:

ctl00_ContentPlaceHolder1_ctlCreateOrder_txtSubTotal
0
 
LVL 40

Expert Comment

by:Kyle Abrahams
ID: 33576771
Note that the control may change.  There is a .clientID on the ASPX side which you can pass to the javascript.
0
 

Author Comment

by:gogetsome
ID: 33576907
Thank you ged325! I have read that but did not know how to actually modify the JavaScript since the textbox and label controls are in a usercontrol on a child page of a mater page... Do you know what I should modify?
0

Featured Post

Visualize your virtual and backup environments

Create well-organized and polished visualizations of your virtual and backup environments when planning VMware vSphere, Microsoft Hyper-V or Veeam deployments. It helps you to gain better visibility and valuable business insights.

Question has a verified solution.

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

Many of us here at EE write code. Many of us write exceptional code; just as many of us write exception-prone code. As we all should know, exceptions are a mechanism for handling errors which are typically out of our control. From database errors, t…
Real-time is more about the business, not the technology. In day-to-day life, to make real-time decisions like buying or investing, business needs the latest information(e.g. Gold Rate/Stock Rate). Unlike traditional days, you need not wait for a fe…
This is Part 3 in a 3-part series on Experts Exchange to discuss error handling in VBA code written for Excel. Part 1 of this series discussed basic error handling code using VBA. http://www.experts-exchange.com/videos/1478/Excel-Error-Handlin…
Michael from AdRem Software explains how to view the most utilized and worst performing nodes in your network, by accessing the Top Charts view in NetCrunch network monitor (https://www.adremsoft.com/). Top Charts is a view in which you can set seve…

762 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