Click here to Skip to main content
15,884,472 members
Please Sign up or sign in to vote.
5.00/5 (1 vote)
See more:
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...
HTML
<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);
       
    } 
</script>
Posted
Updated 31-Jan-12 15:28pm
v2
Comments
Chris Maunder 31-Jan-12 21:26pm    
What does the markup look like? Specifically, what is "lblTotal"?
Sergey Alexandrovich Kryukov 31-Jan-12 21:35pm    
Right; if looks like this is the only option: there is no element with the attribute id="lblTotal".
--SA
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).

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.
 
Share this answer
 
Hi,
You are using

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


Replace innerHTML to innerText. because for LABEL not use innerHTML, use only innerText.
 
Share this answer
 
 
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