Click here to Skip to main content
15,901,938 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
I'm having trouble finding the id of a control within gridview using javascript.
The error I am getting is "undefined".

JavaScript
for (j = 0; j < grid1.rows.length - 1; j++) {
        var total = document.getElementById("GridView1_lblTotal_" + j);
        alert(total.value)
    }


the lblTotal is created clientside.

The markup is

ASP.NET
<ItemTemplate>
   <asp:Label ID="lblTotal" runat="Server"  Width="106"  />
</ItemTemplate>


The source code shows the following...

HTML
<table cellspacing="0" rules="all" border="1" id="GridView1" style="border-collapse:collapse;margin-removed 0px">
		<tr>
			<th align="left" scope="col">Devices And Services</th><th scope="col">Rate</th><th scope="col">Number</th><th scope="col">Total</th>
		</tr><tr>
			<td style="background-color:#CCCCCC;">
                        <span id="GridView1_lblItemDescription_0" style="display:inline-block;width:382px;">HP Thin Client or WYSE terminal</span>
                    </td><td>
                        <span id="GridView1_lblRate_0" style="display:inline-block;width:50px;">$32.20</span>
                       
                    </td><td>
                        <input name="GridView1$ctl02$txtBoxNumber" type="text" id="GridView1_txtBoxNumber_0" onchange="multiply(32.2, 'GridView1_txtBoxNumber_0','GridView1_lblTotal_0')" style="background-color:#FFFFCC;width:100px;" />
                    </td><td>                  
                        <span id="GridView1_lblTotal_0" style="display:inline-block;width:106px;"></span>
                    </td>
		</tr><tr>
			<td style="background-color:#CCCCCC;">
                        <span id="GridView1_lblItemDescription_1" style="display:inline-block;width:382px;">PC used as a RDP or ICA Thin Client</span>
                    </td><td>
                        <span id="GridView1_lblRate_1" style="display:inline-block;width:50px;">$43.90</span>
                       
                    </td><td>
                        <input name="GridView1$ctl03$txtBoxNumber" type="text" id="GridView1_txtBoxNumber_1" onchange="multiply(43.9, 'GridView1_txtBoxNumber_1','GridView1_lblTotal_1')" style="background-color:#FFFFCC;width:100px;" />
                    </td><td>                  
                        <span id="GridView1_lblTotal_1" style="display:inline-block;width:106px;"></span>
                    </td>
		</tr><tr>
			<td style="background-color:#CCCCCC;">
                        <span id="GridView1_lblItemDescription_2" style="display:inline-block;width:382px;">PC in a Traditional Network</span>
                    </td><td>
                        <span id="GridView1_lblRate_2" style="display:inline-block;width:50px;">$58.50</span>
                       
                    </td><td>
                        <input name="GridView1$ctl04$txtBoxNumber" type="text" id="GridView1_txtBoxNumber_2" onchange="multiply(58.5, 'GridView1_txtBoxNumber_2','GridView1_lblTotal_2')" style="background-color:#FFFFCC;width:100px;" />
                    </td><td>                  
                        <span id="GridView1_lblTotal_2" style="display:inline-block;width:106px;"></span>
                    </td>
		</tr><tr>
Posted

C#
for (j = 0; j < grid1.rows.length - 1; j++) {
           var total ="GridView1_lblTotal_"+ j;
           alert(document.getElementById(total).textContent;)
       }



Try this

I hope it will work
 
Share this answer
 
 
Share this answer
 
Try This Code

JavaScript
var grid1= document.getElementById("<%= grd1.ClientID %>");

for (j = 0; j < grid1.rows.length - 1; j++) {

      var total = grd.getElementsById("<%=lblTotal.ClientID %>").value + J;
      alert(total)
   }

OR
JavaScript
var grid1= document.getElementById("<%= grd1.ClientID %>");
 for (j = 0; j < grid1.rows.length - 1; j++) {

       var total = grd.getElementsById("<%=lblTotal.ClientID %>").value;
       var result = total + j;
       alert(result)
    }


Accept solution or vote
If this will help you
Thanks
 
Share this answer
 
v2
Your gridview is in ASP.NET. You are searching a table. Because it's generated by ASP.NET, the ID you assigned is not the one used. Use the ClientID property of the GridView to pass the client side Id down to the client
 
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