Grab first row and first column gridview Javascript

Asked

Viewed 342 times

1

I’m trying to get the value of the first line and the first column of gridview through javascript, but it’s returning error. Keep going like I’m trying to do:

var grid = document.getElementById("#<%=GridView12.ClientID %>");
var cell = grid.rows[parseInt(selectedRowIndex) + 1].cells[0];
var hidID = cell.childNodes[0];
document.getElementById('textcodigo').value = hidID;

Returns me the following error:

Uncaught Typeerror: Cannot read Property 'Rows' of null

I managed to reach this code:

var grid = document.getElementById("<%=GridView12.ClientID %>");
var cell = grid.rows[0].cells[0];
var hidID = cell.childNodes[0];
document.getElementById('textcodigo').value = hidID;

But in textcode appears [Object Text]. I need the first line, the first column.

This is my Gridview12:

<asp:GridView ID="GridView12" runat="server" AutoGenerateColumns="False" DataKeyNames="identificador" CellPadding="4" CssClass="table table-responsive table-hover table-striped" GridLines="None" OnRowDeleting="GridView12_RowDeleting1">
  <Columns>
    <asp:BoundField ItemStyle-Width="100px" DataField="identificador" HeaderText="Identificador" SortExpression="identificador"></asp:BoundField>
    <asp:TemplateField>
      <ItemTemplate>
        <asp:LinkButton CssClass="btn btn-danger btn-sm" ID="deleteButton" runat="server" CommandName="Delete" Text="Excluir" OnClientClick="return confirm('Deseja excluir o identificador selecionado?');"></asp:LinkButton>
      </ItemTemplate>
      <ItemStyle Width="10px" />
    </asp:TemplateField>
  </Columns>
</asp:GridView>

And I arrived in this role, but he keeps returning me Undefined.

var grid = document.getElementById("<%=GridView12.ClientID %>");
var cellPivot;

if (grid.rows.length > 0) {

  var theDropdown = grid.rows[0].cells[0].text;
  document.getElementById('textcodigo').value = theDropdown;

}

  • document.getElementById failed to capture an element with the ID informed, that’s because you’re using #.

  • Apart from #, you returned another error. " Uncaught Referenceerror: selectedRowIndex is not defined"

  • Signal that worked and the other is now on the subsequent line. The error says that the variable selectedRowIndex does not exist, ie it was not declared before this line.

  • @Valdeirpsr I need to take the first row, and the first column, I can get var Cell = grid.Rows[0]. Cells[0]; var hidID = Cell.childNodes[0]. text; but it is still returning error, the correct value does not appear. Is appearing Undefined.

  • You can try a for or foreach to go through all the values you want. Since the code snippet is small, we have no idea of the element.

  • Make sure this thread can help you: https://stackoverflow.com/a/31724146/1272142

  • I only need the first row, and the first column. I don’t need a for or foreach. But it is not giving, now appears like this in the text: [Object Text]

  • @Valdeirpsr edited with the code I arrived, but is not passing the value of the first column and the first line.

  • You can use hidID.textContent, hidID.innerHTML, hidID.outerHTML to capture the value of the element cell.childNodes[0]

  • The return he brings me in textcode is UNDEFINED. It’s not bringing any value.

  • This here document.getElementById('textcodigo').value; is returning [object Text]?

  • @dvd yes, and when I put the innerHTML it returns Undefined.

  • He is a Gridview

  • @dvd I’m taking the first column, the first row of the grid, and I want to assign to that value, only it’s not working. I need that data and play in the text code.

  • This grid is an html?

  • @dvd, yes, in all the ways I try to get the value, returns me Undefined. All the examples that me Aseio, returns me this value.

  • You can show this HTML, only until the part of what you want to take?

  • I edited the question with the Gridview12 code and the function changes. Thank you.

Show 13 more comments
No answers

Browser other questions tagged

You are not signed in. Login or sign up in order to post.