views:

58

answers:

3

I have a datagrid and there is a checkbox which is template in each row. Suppose I am in CheckedChanged event of one of the checkboxes. Is there any way I can tell in which row of the datagrid that check box is in?

+1  A: 

Hey,

You can through the Parent property, though you have to do something like chk.Parent.Parent and so on. I don't know how many parent references up the current row is...

HTH.

Brian
A: 

try something like this:

<script language="javascript" type="text/javascript">


        function rowno(rowindex) {
            var gridViewCtlId = document.getElementById("<%=GridView2.ClientID %>").rows[rowindex].cells[1].innerText;
            alert('you clicked on ' + gridViewCtlId);
        }

    </script>

<asp:GridView ID="GridView2" runat="server" OnRowDataBound="GridView1_RowDataBound1" PageSize="5">
        <Columns>
            <asp:TemplateField>
                <ItemTemplate>
                    <%--<asp:Button ID="Button1" runat="server" Text="Button" />--%>
                    <asp:CheckBox ID="CheckBox1" runat="server"  />
                </ItemTemplate>
            </asp:TemplateField>
        </Columns>
    </asp:GridView>

in .cs

protected void GridView1_RowDataBound1(object sender, GridViewRowEventArgs e)
        {
            if (e.Row.RowType == DataControlRowType.DataRow)
            {
                CheckBox delete = (CheckBox)e.Row.Cells[0].Controls[1];
                delete.Attributes.Add("onclick", "javascript:rowno(" + count + ")");
                count++;
            }
        }
anishmarokey
A: 

Ok I found a neat solution :) In the checkedChanged event I just wrote the following:

((GridViewRow)((Control)sender).Parent.Parent).DataItemIndex;
Greg
This is pretty much exactly what @Brian said to do...
Josh