gogetsome
asked on
Asp.net Textbox onChange
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?
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?
You can use javascript . . . also AJAX might take care of the postback issue for you as well.
@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...
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...
Very well you can go with javascript , thats pretty simple instead of making a post back to perform this simple operation .
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
ASKER
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(' txtSubtota l').value !="")
Sum = Sum + Number(document.getElement ById('txtS ubtotal'). value);
if (document.getElementById(' txtTax').v alue !="")
Sum = Sum + Number(document.getElement ById('txtT ax').value );
if (document.getElementById(' txtMisc'). value !="")
Sum = Sum + Number(document.getElement ById('txtM isc').valu e);
if (document.getElementById(' txtShippin g').value !="")
Sum = Sum + Number(document.getElement ById('txtS hipping'). value);
document.getElementById('l blTotal'). innerHTML = Sum;
return false;
}
</script>
I put this in each textbox control:
onchange="javascript:retur n CalculateTotal();"
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('
Sum = Sum + Number(document.getElement
if (document.getElementById('
Sum = Sum + Number(document.getElement
if (document.getElementById('
Sum = Sum + Number(document.getElement
if (document.getElementById('
Sum = Sum + Number(document.getElement
document.getElementById('l
return false;
}
</script>
I put this in each textbox control:
onchange="javascript:retur
ASKER
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...
ASKER
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_ ctlCreateO rder_txtSu bTotal
For example:
ctl00_ContentPlaceHolder1_
Note that the control may change. There is a .clientID on the ASPX side which you can pass to the javascript.
ASKER
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?