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
2010-09-21 12:46:20
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
2010-09-21 12:50:06
A:
Ok I found a neat solution :) In the checkedChanged event I just wrote the following:
((GridViewRow)((Control)sender).Parent.Parent).DataItemIndex;
Greg
2010-09-21 12:56:57
This is pretty much exactly what @Brian said to do...
Josh
2010-09-21 13:37:16