views:

1335

answers:

2

Hi!

In an ASP:ListView control, I have a checkbox for each row. Each row represents a product with a corresponding price. I want the customer to select one or more products and calculate a total price client side.

So far, I've used the following code, but it does not work:

<asp:CheckBox ID="CheckBox" 
              runat="server" 
              Text="" 
              Checked='<%# Convert.ToBoolean(Eval("Selected")) %>'
              onchange="changeTotal(this, <%# Eval("Price")) %>)"
/>

I have a javascript function changeTotal(referer, value) which should determine if the product was just selected and add/subtract the product price from a total.

The PROBLEM is the javascript function assignment in the CheckBox definition - what is wrong with this approach? Why can't I assign a static price for the javascript function? When I remove the <%# Eval("Price")) %> part from the checkbox, the code compiles without errors.

+1  A: 

Change your double quotes in the onchange to single quotes.

Paddy
Actually I thought I had tried that, but it does remove the compiler error. But the resulting client code seems wrong. I get the javascript function attached to a <span> with the checkbox inside, and the <%# Eval("Price") %> hasn't been evaluated. How should I formulate my checkbox properties?
Chau
Here's one way, bit hacky, but hey...http://www.michaelteper.com/archive/2009/03/03/add-client-side-onchange-handler-to-asp.net-checkbox-control.aspx
Paddy
Hi Paddy, I will try the solution in your link. Thanks for your efforts :)
Chau
A: 
<script language="javascript" type="text/javascript">

function validatecheckbox(mode) { //alert('mode' + mode); //mode : 1 From : chkdeRegister //mode : 2 From : chkcancel var chkdeRegister = document.getElementById ("<%=chkdeRegister.ClientID%>"); var chkcancel = document.getElementById("<%=chkcancel.ClientID%>");

//alert('chkdeRegister: ' + chkdeRegister.checked); //alert('chkcancel: ' + chkcancel.checked); if(mode==1) { if(chkdeRegister.checked) { chkcancel.checked=false; } } else { if(chkcancel.checked) { chkdeRegister.checked=false; } } }

lavanyavathi.bharathidhasan
What does this mean?
Chau