Rahul Khanna

Rahul Khanna

  • NA
  • 236
  • 0

getting error calculting the total of gridview cells using javascript

May 23 2011 6:30 AM
Hi all,
I am calculating the total of one gridivew cells using javascript and displaying the resul in gridivew
footer. but i am getting this exception
Microsoft JScript runtime error: 'document.getElementById(...)' is null or not an object

Here is my code:
<asp:GridView ID="gv" runat="server" AutoGenerateColumns="false" ShowFooter="true"> 
   <Columns>      
       <asp:TemplateField>
          <ItemTemplate>
             <asp:TextBox ID="TextBox1" runat="server" onkeyup="Calculate('gv')" />
         </ItemTemplate>
         <FooterTemplate>
            <asp:TextBox ID="total" runat="server" Text="0" Width="83px"></asp:TextBox>
         </FooterTemplate>
   </Columns>
</asp:GridView>

 <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[2].childNodes[2]; //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
}


Answers (10)