• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 471
  • Last Modified:

Updating a value when checkbox is unchecked

I have a checkbox in a grid and for its OnClick event, i call a javascript that adds values together. This section of it works.

Now, even when I uncheck the checkbox, the javascript is called and values are still added. I dont want to add when  checkbox is unchecked...i want to decrement...

Not sure how to fix this;

<asp:CheckBox runat="server" onclick='<%# String.Format("javascript:prices(\"{0}\")", Eval("price")) %>'     ID="chkSelect" />

<script type="text/javascript">
     var amount = 0.00;
     function prices(price) {

       
         var tempAmount = document.getElementById('<%=x_amount.ClientID %>');
          // alert(tempAmount.value);
         amount = parseFloat(price) + parseFloat(tempAmount.value);
        //  alert(amount);

         tempAmount.value = parseFloat(amount);
         // alert(amount.value);
        // return false;


     }
  </script>

Open in new window

0
Camillia
Asked:
Camillia
  • 8
  • 5
1 Solution
 
TomRScottCommented:
Once the form is posted, the value of the check box(s) can be evaluated and you can increment/decrement based on that/those value(s) at that time.

However, I think what you really should use is a pair of buttons "+" and "-".

 - Tom
0
 
leakim971PluritechnicianCommented:
replace : <asp:CheckBox runat="server" onclick='<%# String.Format("javascript:prices(\"{0}\")", Eval("price")) %>'     ID="chkSelect" />
by : <asp:CheckBox runat="server" onclick='prices(this)' value='<%# Eval("price") >'    ID="chkSelect" />

and use :
<script type="text/javascript">
	var amount = 0.00;
	function prices(checkbox) {	
		var v = checkbox.checked?checkbox.value:-checkbox.value;
		var tempAmount = document.getElementById('<%=x_amount.ClientID %>');
		// alert(tempAmount.value);
		amount = parseFloat(checkbox.value) + parseFloat(tempAmount.value);
		//  alert(amount);
		tempAmount.value = parseFloat(amount);
		// alert(amount.value);
		// return false;
	}
</script>

Open in new window

0
 
CamilliaAuthor Commented:
I get NaN for value. I put alerts to debug and I click on a checkbox and these are the values I see...not sure why i get 0.00

 <script type="text/javascript">     var amount = 0.00;
     function prices(checkbox) {

         alert(checkbox); //I get 50 ...the value i passed
         var v = checkbox.checked ? checkbox.value : -checkbox.value;
         var tempAmount = document.getElementById('<%=x_amount.ClientID %>');
          alert(tempAmount.value); //i get 0.00
         amount = parseFloat(checkbox.value) + parseFloat(tempAmount.value);
           alert(amount); //I get NaN
         tempAmount.value = parseFloat(amount);
         alert(tempAmount.value); //I get NaN
         // return false;
     }
</script>
0
Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
leakim971PluritechnicianCommented:

  alert(checkbox); //I get 50 ...the value i passed

You MUST have :
<asp:CheckBox runat="server" onclick='prices(this)' value='<%# Eval("price") >'    ID="chkSelect" />
0
 
CamilliaAuthor Commented:
1. Ok, I got the NaN figured out but still doesnt work. I used your Javascript with my line of checkbox:
 <asp:CheckBox runat="server" onclick='<%# String.Format("javascript:prices(\"{0}\")", Eval("price")) %>'     ID="chkSelect" />

This got rid of the NaN value but still it doesnt decrement

2. I used your Javascript with our line of checkbox
<asp:CheckBox runat="server" onclick='prices(this)' value='<%# Eval("Price") %>'      ID="chkSelect" />

I get "NaN" for values. The first alert gives me "InputHTMLElement", then 0.00, then NaN, NaN.

I think it's the checkbox Value that's not being passed?
0
 
CamilliaAuthor Commented:
asp.net's checkbox doesnt have "value" as a property. I wonder if I need to use this

  <checkbox  runat="server" onclick='prices(this)' value='<%# Eval("Price") %>'      ID="chkSelect"></checkbox>
0
 
leakim971PluritechnicianCommented:
The first alert gives me "InputHTMLElement"(good), then 0.00(good) , then NaN, NaN

look like the eval don't work...
what do you get with :

function prices(checkbox) {
alert(checkbox.value); // add this to display the value of the checkbox

?
0
 
CamilliaAuthor Commented:
I get "on" (no double quotes)

 I have this on the same page and this works

<asp:Label ID="lblPrice" runat="server" Text='<%# Eval("Price") %>'></asp:Label>

...i wonder if I should change "asp:checkbox" to checkbox
0
 
leakim971PluritechnicianCommented:
another one :
<asp:CheckBox runat="server" onclick='<%# String.Format("javascript:prices(this,\"{0}\")", Eval("price")) %>'     ID="chkSelect" />
with :
<script type="text/javascript">
	var amount = 0.00;
	function prices(checkbox, v) {
		v = checkbox.checked?v.value:-v.value;
		var tempAmount = document.getElementById('<%=x_amount.ClientID %>');
		// alert(tempAmount.value);
		amount = parseFloat(v) + parseFloat(tempAmount.value);
		//  alert(amount);
		tempAmount.value = parseFloat(amount);
		// alert(amount.value);
		// return false;
	}
</script>

Open in new window

0
 
CamilliaAuthor Commented:
let me try and i will post back
0
 
CamilliaAuthor Commented:
I still get NaN:

  alert(checkbox); //HTMLInputElement
        alert(v);  //i get 50
        v = checkbox.checked ? v.value : -v.value;
        var tempAmount = document.getElementById('<%=x_amount.ClientID %>');
        alert(tempAmount.value); //0.00
        // alert(tempAmount.value);
        amount = parseFloat(v) + parseFloat(tempAmount.value);
         alert(amount); //NaN
        tempAmount.value = parseFloat(amount);
        alert(tempAmount.value); //NaN
0
 
leakim971PluritechnicianCommented:
corrected :
<script type="text/javascript">
	var amount = 0.00;
	function prices(checkbox, v) {
		v = checkbox.checked?v:-v;
		var tempAmount = document.getElementById('<%=x_amount.ClientID %>');
		// alert(tempAmount.value);
		amount = parseFloat(v) + parseFloat(tempAmount.value);
		//  alert(amount);
		tempAmount.value = parseFloat(amount);
		// alert(amount.value);
		// return false;
	}
</script>

Open in new window

0
 
CamilliaAuthor Commented:
This comes as NaN

 alert(parseFloat(v));  //NaN
        alert(parseFloat(tempAmount.value)); //this is zero
0
 
CamilliaAuthor Commented:
We posted at the same time. Let me try the one u posted
0

Featured Post

Concerto Cloud for Software Providers & ISVs

Can Concerto Cloud Services help you focus on evolving your application offerings, while delivering the best cloud experience to your customers? From DevOps to revenue models and customer support, the answer is yes!

Learn how Concerto can help you.

  • 8
  • 5
Tackle projects and never again get stuck behind a technical roadblock.
Join Now