Access Gridview cell from Javascript
I've looked at the other post regarding the Same Question
But this is insufficient for what I have to ask about.
It's very simple actually, I want to access the cell value from Javascript.
I have tried this:
var grid = document.getElementById('<%=gvUdgivelser.ClientID%>');
var cell = grid.rows[0].cells[5].innerText;
But I get a undefined
in return. If I use the innerHTML
I get a result with the full HTML, which I am n开发者_Python百科ot interested in.
However, how do I pass a rowIndex
to a function from the gridview button, in order for the code to find out which row was clicked?
try this :
var grid = document.getElementById('<%=gvUdgivelser.ClientID%>');
var cell = grid.rows[0].cells[5].textcontent;
hope this help
If you don't mind using JQuery, you can retrieve the cells value like so:
var rowIndex = 1;
var cellIndex = 5;
var cellValue = $('#<%= gvUdgivelser.ClientID %> tbody tr:eq(' + rowIndex + ') td:eq(' + cellIndex + ')').html();
As for determining the row index, you can do this by creating a button within the grid view:
<asp:TemplateField>
<ItemTemplate>
<input type="button" onclick="getRowIndex(this);" value="Get Row Index" />
</ItemTemplate>
</asp:TemplateField>
And then make use of JQuery again to determine the row index:
function getRowIndex(button) {
var closestRow = $(button).closest('tr');
var rowIndex = $('tr').index(closestRow);
}
Hope this helps.
精彩评论