GridView从行寻找到该控件,以及从该控件获知该行
原文发布时间为:2008-08-01 —— 来源于本人的百度文章 [由搬家工具导入]
GridView从行寻找到该控件:
protected void Button3_Click(object sender, EventArgs e)
{
foreach (GridViewRow gr in GridView1.Rows)
{
CheckBox chk = (CheckBox) gr.Cells[4].FindControl("chk2");//在第4列中寻找对象,并转为Checkbox
if (chk.Checked)
{
gr.BackColor = System.Drawing.Color.Red;
Response.Write(gr.Cells[1].Text);
}
}
}
----------------------------------
从该控件获知该行:
protected void chk2_CheckedChanged(object sender, EventArgs e)
{
CheckBox chk = (CheckBox)sender;//把对象为Checkbox
DataControlFieldCell dcf = (DataControlFieldCell)chk.Parent;//这个对象的父类为cell
GridViewRow gr = (GridViewRow)dcf.Parent;//cell的父类就是row,这样就得到了该checkbox所在的该行
if (chk.Checked)
{
gr.BackColor = System.Drawing.Color.Green;
}
else
{
gr.BackColor = GridView1.RowStyle.BackColor;
}
}