views:

642

answers:

3

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

A: 

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>
iburlakov
A: 

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

Jason Berkan
A: 

you can add javascript function on rowdatabound and set parameter row index like...

protected void grd_RowDataBound(object sender, GridViewRowEventArgs e)
{
    if (e.Row.RowType == DataControlRowType.DataRow)
    {
        ((Button)e.Row.FindControl("yourButtonID")).Attributes.Add("onclick", "javascript:JSfunctionName(" + (e.Row.RowIndex) + ");");
    }
}

<script language="javascript" type="text/javascript">
function JSFunctionName(ri)
{}
Muhammad Akhtar