gridview更新数据时出现未将对象引用设置到对象的实例问题

gridview更新数据时出现未将对象引用设置到对象的实例问题

问题描述:

源错误:

行 89: {
行 90: //pet.ID = Convert.ToInt16((GridView1.Rows[e.RowIndex].FindControl("Label1")as Label).Text);

行 91: pet.Kind = (GridView1.Rows[e.RowIndex].FindControl("TextBox2") as TextBox).Text;
行 92: pet.KindNumber = (GridView1.Rows[e.RowIndex].FindControl("TextBox3") as TextBox).Text;
行 93: pet.Heat = Convert.ToInt16((GridView1.Rows[e.RowIndex].FindControl("Label1") as Label).Text);

源文件: F:\vsproject\PetCare\PetCare\WebForm1.aspx.cs 行: 91

调试下,GridView1.Rows[e.RowIndex].FindControl("TextBox2")看看是不是输出的是null
看下你控件的嵌套关系,是否有TextBox2

我觉得是 GridView1.Rows[e.RowIndex].FindControl("TextBox2") as TextBox的问题,但我不知道怎么改,纯新手,希望大家帮帮忙