0

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.

Hema
  • 21
  • 3

3 Answers3

0

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.

VDWWD
  • 32,238
  • 19
  • 56
  • 70
0

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

querySelector, wildcard element match?

Community
  • 1
  • 1
Lain
  • 3,117
  • 1
  • 13
  • 23
0

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>