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 theID
informed, that’s because you’re using#
.– Valdeir Psr
Apart from #, you returned another error. " Uncaught Referenceerror: selectedRowIndex is not defined"
– Mariana
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.– Valdeir Psr
@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.
– Mariana
You can try a
for
orforeach
to go through all the values you want. Since the code snippet is small, we have no idea of the element.– Valdeir Psr
Make sure this thread can help you: https://stackoverflow.com/a/31724146/1272142
– Jr. Pacheco
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]
– Mariana
@Valdeirpsr edited with the code I arrived, but is not passing the value of the first column and the first line.
– Mariana
You can use
hidID.textContent
,hidID.innerHTML
,hidID.outerHTML
to capture the value of the elementcell.childNodes[0]
– Valdeir Psr
The return he brings me in textcode is UNDEFINED. It’s not bringing any value.
– Mariana
This here
document.getElementById('textcodigo').value;
is returning[object Text]
?– Sam
@dvd yes, and when I put the innerHTML it returns Undefined.
– Mariana
He is a Gridview
– Mariana
@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.
– Mariana
This grid is an html?
– Sam
@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.
– Mariana
You can show this HTML, only until the part of what you want to take?
– Sam
I edited the question with the Gridview12 code and the function changes. Thank you.
– Mariana