Click here to Skip to main content
15,891,777 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
Hello.

I'm looking to total a gridview column using jquery.
I have a javascript function that does the initial calculations then calls the calculate jquery function that is supposed to total the column.

Getting an object expected error at present.

Javascript calculation code below.

JavaScript
function multiply(Rate, number, total) {

       var grid = document.getElementById('GridView1');
       var num = parseFloat(document.getElementById(number).value);
       var tot = document.getElementById(total);

       var totValue = parseFloat(((Rate * num)));
       var totValueRound = Math.round(totValue);
       var totValueRound = totValue;
       tot.innerHTML = "$" + totValue.toFixed(2);
       calculate();


Jquery code to calculate the column below.

JavaScript
<script src="jquery-1.7.1.min.js" type="text/javascript"></script>
    <script language="javascript" type="text/javascript">
        
        function calculate() {
            var values = 0;
            $(".total").each(function(){
            values += parseInt($(this).html());
            $("#lblTotalAmount").html(values);  
        }
    </script>


Mark up below

ASP.NET
 <asp:GridView ID="GridView1" runat="server"
AutoGenerateColumns="False" OnRowDataBound="GridView1_RowDataBound" 
            DataSourceID="AccessDataSource1" 
            ShowFooter="True" style="margin-bottom: 0px">
            <Columns>
            <asp:TemplateField HeaderText="Devices And Services" Visible="True" HeaderStyle-HorizontalAlign="Left"  FooterText="Payable monthly in advance (excluding Agreement items):" ItemStyle-BackColor="#CCCCCC">
                    <ItemTemplate>
                        <asp:Label ID="lblItemDescription" runat="server" Width="382"
Text='<%# Eval("itemDescription") %>'></asp:Label>
                    </ItemTemplate>

<HeaderStyle HorizontalAlign="Left"></HeaderStyle>
                </asp:TemplateField>
                <asp:TemplateField HeaderText="Rate">
                    <ItemTemplate>
                        <asp:Label ID="lblRate" runat="server" width="50" ></asp:Label>
                       
                    </ItemTemplate>
                </asp:TemplateField>
                <asp:TemplateField HeaderText="Number" ControlStyle-BackColor="#FFFFCC">
                    <ItemTemplate>
                        <asp:TextBox ID="txtBoxNumber" Width="100px"
runat="Server"  />
                    </ItemTemplate>
                </asp:TemplateField>
                <asp:TemplateField HeaderText="Total" ItemStyle-CssClass="total">
                    <ItemTemplate>                  
                        <asp:Label ID="lblTotal" runat="Server"  Width="106"  />
                    </ItemTemplate>
                    <FooterTemplate>
                          
                 <asp:Label ID="lblTotalAmount" runat="server"  />        
                
                    </FooterTemplate>
                </asp:TemplateField>
              
               
                  
                    
                
            </Columns>
            <FooterStyle Font-Bold="True" />
        </asp:GridView>
Posted
Updated 8-Feb-12 19:54pm
v4
Comments
zyck 7-Feb-12 22:32pm    
can you provide your sample code
Andrew Chambers 8-Feb-12 23:12pm    
Code is now up.

1 solution

After getting rendered the gridview behaves like a simple html table. You can view your page source and from there you can find the ID of the table and then you can do your work with javascript... Or you can go with the server side event also.. But as I think you should go with javascript only...
 
Share this answer
 
Comments
Tech Code Freak 9-Feb-12 0:43am    
5up!

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