How to get row index of a gridview row clicked by a user on it by javascript?
How to get row index of a gridview row clicked by a user on it开发者_开发百科 by javascript ?
Here is a little sample:
<asp:GridView ID="GridView1" runat="server">
<Columns>
<asp:TemplateField>
<ItemTemplate>
<input type="button" value="getIndex" onclick="getIndex(<%# Container.DataItemIndex %>);" />
</ItemTemplate>
</asp:TemplateField>
</Columns>
</asp:GridView>
<script type="text/javascript">
function getIndex(index) {
alert(index);
}
</script>
Give every row an unique id
with the same suffix.
As example:
<table>
<tbody>
<tr id="row_1">
<td>
<input onpasteventorwhatever="related_function(event,1)">
</input>
</td>
</tr>
</tbody>
</table>
I believe the answer to this older question will provide the answer you need.
<asp:GridView ID="GridView1" runat="server" AllowPaging="True" AllowSorting="True"
onclick="EditCell(event);"..............
<script type="text/javascript">
function EditCell( e) {
var rowIndex = e.srcElement.parentElement.sectionRowIndex;
var cell = document.getElementById("GridView1").rows[rowIndex].cells[e.srcElement.cellIndex].innerText;
alert(cell);
}
</script>
精彩评论