Click here to Skip to main content
15,886,806 members
Please Sign up or sign in to vote.
5.00/5 (1 vote)
See more:
Hi all,
I am struggling a lot to find out the total of gridview cells and displaying the values in a footer textbox. Every time I am getting an exception:
Microsoft JScript runtime error: Object expected

Here is my gridview code:
XML
<div>
<asp:gridview ID="Gridview1" runat="server" ShowFooter="true"
            onrowcommand="Gridview1_RowCommand"  AutoGenerateColumns="false"
            CellSpacing="0" CellPadding="0" Font-Bold="false"
            onrowdeleting="Gridview1_RowDeleting">
        <Columns>
      <asp:BoundField DataField="RowNumber" HeaderText="Row Number"/>
  <asp:TemplateField HeaderText="Select" ControlStyle-Width="50px" HeaderStyle-Font-Bold="false" ControlStyle-Font-Bold="false">
     <ItemTemplate>
         <asp:CheckBox ID="chkSelect" runat="server" Width="80px"/>
     </ItemTemplate>
   </asp:TemplateField>
        <asp:TemplateField HeaderText="Header 1" HeaderStyle-Font-Bold="false" ControlStyle-Font-Bold="false">
            <ItemTemplate>
                <asp:TextBox ID="TextBox1" runat="server" Width="70px"></asp:TextBox>
            </ItemTemplate>
            <FooterTemplate>
                <asp:Label ID="lblTotal" runat="server" Text="Total" Font-Bold="true"></asp:Label>
            </FooterTemplate>
        </asp:TemplateField>
        <asp:TemplateField HeaderText="Header 2"  HeaderStyle-Font-Bold="false" ControlStyle-Font-Bold="false">
            <ItemTemplate>
                <asp:TextBox ID="TextBox2" Width="70px" runat="server" onkeyup="Calculate('Gridview1')"></asp:TextBox>
            </ItemTemplate>
            <FooterTemplate>
                <asp:TextBox ID="total" runat="server" Width="70px"></asp:TextBox>
            </FooterTemplate>

        </asp:TemplateField>
        <asp:TemplateField HeaderText="Header 3" HeaderStyle-Font-Bold="false" ControlStyle-Font-Bold="false">
            <ItemTemplate>
                 <asp:TextBox ID="TextBox3" Width="70px" runat="server" ></asp:TextBox>
            </ItemTemplate>
            <FooterStyle HorizontalAlign="Right" />
            <FooterTemplate>
             <asp:Button ID="ButtonAdd" runat="server" Text="Add New Row" CommandName="AddNewRow" />
            </FooterTemplate>
        </asp:TemplateField>
        </Columns>
</asp:gridview>
    </div>


Here is my JavaScript code:
JavaScript
<script type="text/javascript" src="http://ajax.aspnetcdn.com/ajax/jquery/jquery-1.5.js"></script>
  <script type="text/javascript" src="http://ajax.aspnetcdn.com/ajax/jquery.ui/1.8.10/jquery-ui.js"></script>
  <script type="text/javascript">
    function Calculate(GridView)
    {
       var total = 0;
       var gridview = document.getElementById('<%=Gridview1.ClientID %>').getElementsByTagName("input");
       for ( i = 0; i < gridview.rows.length; i ++)
       {
           var node = gridview.rows[i].cells[3].childNodes[3]; //textbox

           if (node != undefined && node.type == "text") //check only textbox, ignore empty one
                if (!isNaN(node.value) && node.value != "") //check for valid number
                   total += parseInt(node.value);
       }
      // document.getElementById("total").innerHTML = total.toString(); //display
       var gridview1 = document.getElementById('<%=grdview1.ClientID %>');
       gridview1.rows[gridview.rows.length -1].cells[0].innerHTML=total;

    }
</script>


I had tried with many ways but every time getting the same exception:
Microsoft JScript runtime error: Object expected

It seems something's wrong with my JavaScript code.
Please somebody help me out.
Posted
Updated 19-Jul-19 21:11pm
v2

Mistake in you code(wrong control name in js code)
This line
var gridview1 = document.getElementById('<%=grdview1.ClientID %>');

should be
var gridview1 = document.getElementById('<%=Gridview1.ClientID %>');

Gridview1 is the ID of the GridView control
 
Share this answer
 
Add a css class to your fields

    <asp:BoundField DataField="Time" HeaderText="Time"> 
      <ItemStyle CssClass="yourclass"></ItemStyle> 
     </asp:BoundField> 

and use the following code

 var fields= document.getElementsByClassName('yourclass');
    var sum = 0;
    for (var i = 0; i < fields.length; ++i) {
        var item = fields[i];  
         sum += parseInt(item.innerHTML);
    }

and then assign that sum to your total label

$("#<%= totalHours.ClientID %>").text(sum);
 
Share this answer
 

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