Js and asp gridviews

User enters integer values into an editable gridview. JS is meant to keep a running total in the gridview footer template while user enters the values. User clicks ‘Save’ button. The values are saved to database table.

Problems:

  1. JS does not keep a running total if there is already values saved in a database table.
    (Explaination: If user clicks ‘select all’, the previously added values are loaded. Can edit these and
    running total works. But if user is entering numbers for the first time into a blank editable gridview,
    the js does not work).

  2. Cannot access label in gridview footer template. ‘Object reference not set to an instance of an object’.
    If label is placed in normal gridview row, it is fine. However, it needs to be in the footer.

javascript function


<script type="text/javascript">
        var sumVal = Number(0);
        function sum(id1,id2) {
            var current = document.getElementById(id1).value;
            sumVal += Number(current);
            document.getElementById(id2).innerText = sumVal;
        }
     
    </script>

javascript snippet from gridview_rowdatabound


 //Javascript controls
TextBox numbers = (TextBox)e.Row.FindControl("NumberTextBox");
numbers.Attributes.Clear();


//Meant to be in footer template but placed in normal row for now                               
Label total = (Label)e.Row.FindControl("total");
total.Attributes.Clear();
 //Sums up the values of each textbox
numbers.Attributes.Add("onblur", string.Format("sum('{0}', '{1}')", numbers.ClientID, total.ClientID ));