Click here to Skip to main content
15,898,222 members
Please Sign up or sign in to vote.
1.50/5 (2 votes)
I want to total every row from my listview

tallies total infected people and percentage
____________________________________________________________________
CityID | City | Population | Male | Female | Total | Percentage |

Population, male, and female columns are user inputs
total column is male+female textbox value per row
percentage= (total/population)*100 also per row
to calculate total and percentage on every textchange

here is my html code:
XML
<asp:ListView ID="ListView1" runat="server">
   <LayoutTemplate>
      <table style="border: solid 2px #336699;" cellspacing="0" cellpadding="3" rules="all">
         <tr style="background-color: #336699; color: White;">

            <th>City</th>
            <th>Population</th>
            <th>Male</th>
            <th>Female</th>
            <th>Total</th>
            <th>%</th>
         </tr>
         <tbody>
            <asp:PlaceHolder ID="itemPlaceHolder" runat="server" />
         </tbody>
      </table>
   </LayoutTemplate>
   <ItemTemplate>
      <tr>
        <td> <asp:Label ID="lblCtyID" runat="server" Text='<%# Bind("CityID") %>' /> </td>
        <td> <asp:Label ID="lblCty" runat="server" Text='<%# Bind("CityName") %>'/> </td>
         <td><asp:TextBox ID="txtPopu" runat="server"/></td>
         <td>
         <asp:TextBox ID="txtMale" runat="server" Width="69px" ValidationGroup="check"/>
         <asp:RegularExpressionValidator ID="RegularExpressionValidator1" Display="Dynamic" runat="server" ForeColor="Maroon" SetFocusOnError="True" ControlToValidate="txtMale" ErrorMessage="X" ValidationExpression="[0-9]*" ValidationGroup="check"></asp:RegularExpressionValidator>
         </td>
         <td><asp:TextBox ID="txtFemale" runat="server" Width="69px" onpaste = "return false;" onkeyup ="keyUP(event.keyCode)" onkeydown = "return isNumeric(event.keyCode);"/></td>
         <td><asp:TextBox ID="txtTotal" runat="server" enabled="false" Width="85px"/></td>
         <td><asp:TextBox ID="txtPercent" runat="server" enabled="false" Width="85px"/></td>

      </tr>
   </ItemTemplate>
</asp:ListView>
        <asp:Button ID="Button1" runat="server" onclick="Button1_Click" Text="Save Info"
            Width="136px" />
<br />
        <asp:SqlDataSource ID="SqlDataSource1" runat="server"
            ConnectionString="<%$ ConnectionStrings:GeraldConnString2 %>"
            SelectCommand="SELECT [CityID], [CityName] FROM [Cities]"></asp:SqlDataSource>
Posted
Updated 28-Nov-10 21:16pm
v3

Hi,

You can solve the above function by writing following javascript fucntion.

Call the below function in Population, male, and female columns on onkeypress event of text boxes as follows.

C#
onkeypress="javascript:return CalculateValue(this);";

function CalculateValue(obj)
{
    var tr=obj.parentNode.parentNode //get the entered row object.
    var txt1=tr.cells[2].getElementByTagName("INPUT")[0] //population textbox

    var txt2=tr.cells[3].getElementByTagName("INPUT")[0] //male textbox
    var txt3=tr.cells[4].getElementByTagName("INPUT")[0] //female textbox
    var txt4=tr.cells[5].getElementByTagName("INPUT")[0] //total textbox
    var txt5=tr.cell[6].getElementByTagName("INPUT")[0] //percentage textbox

txt4.value=parseInt(txt2.value) + parseInt(txt3.value); //total value calculation.

txt5.value= (parseInt(txt4.value) / parseInt(txt1.value)) * 100 //percentage calculation.

}


Make sure that by default your textboxes need to have zero.

Regards,
Kiran.
 
Share this answer
 
v2
Comments
gherard 29-Nov-10 7:02am    
I dont get this one, do I have to substitute values for this one?
does obj stands for the listview1(name)?
please explain. thank you.


var tr=obj.parentNode.parentNode
Kirankumar Ballapalli 29-Nov-10 7:13am    
obj means your textboxes object.
This will give the current textbox object and then it will traverse for finding current row in the form of parentNode.call like this in textbox
onkeypress="javascript:return CalculateValue(this);";
if you put in alert like (obj.id),you will get the control id of textbox.
Kirankumar Ballapalli 29-Nov-10 7:18am    
Its not getElementByTagName.
The correct is getElementsByTagName
gherard 1-Dec-10 3:09am    
one more question how about if you want two call two javascript function on an event like on keydown I would be calling the numbervalidator function and CalculateValue without joining them on one function?
CSS
<pre lang="Javascript">><<small><code><big><pre lang="Delphi"><pre lang="text"><pre lang="c++"><pre lang="HTML"><pre lang="Javascript">
 
Share this answer
 
http://img26.imageshack.us/img26/4043/listview.png[^]

onkeypress was not valid attribute so I used onkeydown.

I have tried your javascript function but it displays nothing

*I have 0 values on my textbox fields as default
*the function is being called but does nothing

I dont get this one, do I have to substitute values for this one?
does obj stands for the listview1(name)?
please explain. thanks.
C#
var tr=obj.parentNode.parentNode


this is the present javascript I have

XML
<script type="text/javascript" language="javascript">
        function CalculateValue(obj)
       {
       var tr=obj.parentNode.parentNode //get the entered row object.
       var txtPopu=tr.cells[2].getElementByTagName("INPUT")[0] //population textbox
       var txtMale=tr.cells[3].getElementByTagName("INPUT")[0] //male textbox
       var txtFemale=tr.cells[4].getElementByTagName("INPUT")[0] //female textbox
       var txtTotal=tr.cells[5].getElementByTagName("INPUT")[0] //total textbox
       var txtPercent=tr.cell[6].getElementByTagName("INPUT")[0] //percentage textbox
       txtTotal.value=parseInt(txtMale.value) + parseInt(txtFemale.value); //total value calculation.
       txtPercent.value= (parseInt(txtTotal.value) / parseInt(txtPopu.value)) * 100 //percentage calculation.
       }
   </script>
 
Share this answer
 
v2
Comments
Kirankumar Ballapalli 29-Nov-10 6:45am    
Hi,are you getting any javascript error? place one alert inside function.And check whether you are getting tr as exact row or check the index positions of your cells.Please mention error what you are getting.
gherard 29-Nov-10 10:32am    
thanks for your reply kiran,
I am slowly getting this to work.
var txtPercent gets undefined which is in cell[6]
now i am confused other var retrieves the right value but in cell[6] it is undefined.

do you have any ideas?
gherard 29-Nov-10 11:53am    
haha..ok I got it. it should have been cells[6]

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900