Click here to Skip to main content
12,757,627 members (37,168 online)
Rate this:
Please Sign up or sign in to vote.
See more: C# ASP.NET Gridview
I would like to be able to assign a javascript variable to a label in a gridview.
At the moment I get the error message

Microsoft JScript runtime error: Unable to set value of the property 'innerHTML': object is null or undefined

My code is below...
<script language="Javascript" type="text/javascript">
    var grandTotal = 0;
    var percentTotal = 0;
    function multiply2(Rate, number, total) {

            var grid = document.getElementById('GridView5');
            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 = "$" + totValueRound.toFixed(2);
            grandTotal += totValue;
            percentTotal = grandTotal * 10 / 100;
            for (j = 0; j < grid.rows.length - 1; j++) {
            grid.rows[grid.rows.length - 1].cells[2].children[0].innerHTML = "$" + grandTotal.toFixed(2);
        document.getElementById('lblTotal').innerHTML = percentTotal.toFixed(2);

Posted 31-Jan-12 16:24pm
Updated 31-Jan-12 16:28pm
Chris Maunder 31-Jan-12 21:26pm
What does the markup look like? Specifically, what is "lblTotal"?
SAKryukov 31-Jan-12 21:35pm
Right; if looks like this is the only option: there is no element with the attribute id="lblTotal".
Sanjay K. Gupta 31-Jan-12 23:22pm
In which line you are getting error.
1. tot.innerHTML = "$" + totValueRound.toFixed(2);
2. grid.rows[grid.rows.length - 1].cells[2].children[0].innerHTML = "$" + grandTotal.toFixed(2);
3. document.getElementById('lblTotal').innerHTML = percentTotal.toFixed(2);

Put your markup code (if possible).
Rate this: bad
Please Sign up or sign in to vote.

Solution 1

In ASP.NET, your controls do not have the value you gave them in your code. This is especially true in a grid. If the label is in a column, then there's no way that each value in that column has the same name, right ? The easy way is to view source and find the client side name. The correct way is to use the ClientId property of the object in question ( in the row data bound event, if it's inside a grid ), so that you emit javascript which knows the correct id to look for, no matter how your code changes.
Rate this: bad
Please Sign up or sign in to vote.

Solution 2

You are using

tot.innerHTML = "$" + totValueRound.toFixed(2);

Replace innerHTML to innerText. because for LABEL not use innerHTML, use only innerText.
Rate this: bad
Please Sign up or sign in to vote.

Solution 3


This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

    Print Answers RSS
Top Experts
Last 24hrsThis month

Advertise | Privacy | Mobile
Web02 | 2.8.170217.1 | Last Updated 1 Feb 2012
Copyright © CodeProject, 1999-2017
All Rights Reserved. Terms of Service
Layout: fixed | fluid

CodeProject, 503-250 Ferrand Drive Toronto Ontario, M3C 3G8 Canada +1 416-849-8900 x 100