在protected void CheckBox1_CheckedChanged事件中写代码:
protected void CheckBox1_CheckedChanged(object sender, EventArgs e)
{
CheckBox check = (CheckBox)sender;
//System.Web.UI.WebControls.TableCell dcf = (System.Web.UI.WebControls.TableCell)check.Parent;
//DataGridItem dgItem = (DataGridItem)dcf.Parent;
DataGridItem dgItem = (DataGridItem)check.Parent.Parent;
string id = dgItem.Cells[1].Text;
if (check.Checked)
{
string ID = dgItem.Cells[1].Text;
string name = dgItem.Cells[2].Text;
}
}
附加:如果要遍历Datagrid中的CheckBox是否选中
for (int j = 0; j < Datagrid.Rows.Count; j++)
{
CheckBox chbox = Datagrid.Rows[j].FindControl("CheckBox1") as CheckBox;
if (chbox.Checked)
{
string ID = Datagrid.Rows[j].Cells[1].Text;
string name = Datagrid.Rows[j].Cells[2].Text;
}
}