How to assign a value to the label which is inside the gridview. I can retrieve the value of a label,but I could not assign a value to the label inside the Gridview in javascript function. Anyone Help me.
-
Does Label mean – Lain Dec 08 '16 at 11:32
-
var grid = document.getElementById("<%= GVViewStudents.ClientID %>"); – Hema Dec 08 '16 at 11:44
-
What is the markup of the label to change in HTML and what value should it get? – Lain Dec 08 '16 at 11:46
3 Answers
Because the Label is inside a GridView, the scope is limited to the GridView and it is not accessible on the rest of the page.
If you look at the HTML, you will see that the ID of the label looks something like this:
<span id="GridView1_Label1_10">Label</span>
A combination of the ID of the GridView, Label and the row number. So you need to change your JavaScript accordingly.

- 35,079
- 22
- 62
- 79
Generally to select runat server tags you could use querySelector() in combination with the attribute id.
var grid = document.getElementById("<%= GVViewStudents.ClientID %>");
This would turn into:
var grid = document.querySelector("[id$=id of the element]")
So let us assume there is the following:
<asp:label runat = 'server' id = 'myfunnylabel' />
To retrieve that tag in Javascript one could do:
var tLabel = document.querySelector("[id$='myfunnylabel']")
If that label is in a repeatable container, like gridview or repeater one could get a list of all of those with a similar attempt:
var tLabels = document.querySelectorAll("[id*='myfunnylabel']")
https://developer.mozilla.org/en-US/docs/Web/API/Document/querySelector
Please try this -
function assignValueToLabel()
{
var grid = document.getElementById("<%=grv.ClientID%>");
var rowIndex = 3; //this is the index of row of gridview in which you change the value of label
var cellIndex = 0; // this is the index of cell of gridview in which cell you pass the label control
var newValue = grid.rows[rowIndex].cells[1].innerHTML; // this is the value which you want to assign to label.
grid.rows[rowIndex].cells[cellIndex].getElementsByTagName("span")[0].innerHTML = newValue;
return false;
}
</script>

- 84
- 5