2

How to get row index of a gridview row clicked by a user on it by javascript ?

4 Answers 4

2

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>
Sign up to request clarification or add additional context in comments.

1 Comment

hi how to get row ID here: <asp:Button ID="BtnSource" runat="server" Text="Source" class="showButton" OnClick='<%# "return SetRowValues("+Eval("ttppcid")+",this.id,"+Eval("Fair")+","+Eval("Good")+","+Eval("Mint")+","+Eval("Poor")+","+Eval("Fair")+")"%>' />
1

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>

Comments

0

I believe the answer to this older question will provide the answer you need.

Comments

0
<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>

1 Comment

To make your answer more useful for other, please provide some insight or explanation for context on your code. For help, see How to Write a Good Answer on StackOverflow.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.