Click here to Skip to main content
15,942,619 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
I Have GridView Columns of Product(Label), Quantity(ASPTextbox), Rate(ASPTextbox) and Amount(Label), whenever i change rate or quantity i want to calculate amount(qty*rate) of product using javascript.

<script type="text/javascript">
        function startCalc() {
            var rate = parseFloat(document.getElementById('<%=gv1.FindControl("txtrate").ClientID%>').value);
            var qty = parseFloat(document.getElementById('<%=gv1.FindControl("txtqty").ClientID%>').value);
            var totalamount = rate * qty;
            document.getElementById('<%=gv1.FindControl("lblamt").ClientID%>').value = totalamount;

I am getting an error at var rate = parseFloat(document.getElementById('<%=gv1.FindControl("txtrate").ClientID%>').value); of object reference not set an instance of an object
Please Help Me

Thanks In Advance
Updated 8-Oct-21 4:16am
Karthik_Mahalingam 29-Apr-15 2:28am    
what you have tried ?
post your code
Ravi Sargam 29-Apr-15 2:42am    
please check above code
gv1.FindControl("txtrate") -> There will be many TextBoxes on the GridView, may be one on each row. Which one you want?

1 solution

Hello Ravi,

Good Day !!!

We may have n number of rows in Gridview we should mention the row index t find the text box value

Modify your script like this it will work
<script type="text/javascript">
        function startCalc() {
           var grid = document.getElementById("<%= gv1.ClientID%>");  
            for (var i = 0; i < grid.rows.length - 1; i++) {  
           var rate =  $("input[id*=txtrate]") ;
           var qty =   $("input[id*=txtqty]") ;
           var totalamount = rate[i].value * qty[i].value;
              $("input[id*=lblamt]") = totalamount[i] .value;
Share this answer
Dinkar Veer 25-Oct-18 4:24am    
Hello @santosh your code is working fine but i am uanable to bind output value to any textbox or label in gridview.

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