How to get values of textbox in listview and perform calculations in javascript?

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • ghe magno
    New Member
    • Nov 2010
    • 1

    How to get values of textbox in listview and perform calculations in javascript?

    image of my listview:
    [imgnothumb]http://img26.imageshac k.us/img26/4043/listview.png[/imgnothumb]

    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:


    Code:
    <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>
    Last edited by Frinavale; Dec 6 '10, 02:43 PM. Reason: Added the image into the thread.
  • Frinavale
    Recognized Expert Expert
    • Oct 2006
    • 9749

    #2
    Well, in JavaScript you need to get a reference to the table that contains your TextBoxes.

    Then you need to loop through each row in the table and retrieve all of the TextBoxes within the row (using the getElementsByTa gName method).

    Then you need to loop through your TextBoxes and retrieve the data that the user provided...conv ert the values entered into numbers and perform your calculations.

    The question is, when do you do this?

    Comment

    • aspdotnetuser
      New Member
      • Nov 2010
      • 22

      #3
      Use JQuery, you feel the power of unobtrusive DOM parsing

      Comment

      Working...