我在GridView放了一个TextBox
protected void GridView1_RowDataBound(object sender, GridViewRowEventArgs e)
    {
        
        TextBox txt = (TextBox)(e.Row.FindControl("TextBox1"));
        string aa = txt.Text;
        Response.Write(aa);    }怎么报错啊,请高手指点

解决方案 »

  1.   

    protected void GridView1_RowDataBound(object sender, GridViewRowEventArgs e)
    {
          if(e.Row.RowIndex>-1)
          {
            TextBox txt = (TextBox)(e.Row.FindControl("TextBox1"));
            string aa = txt.Text;
            Response.Write(aa);
          }
     }
      

  2.   

    报错的原因是因为绑定Header/Footer/Pager等项时也会触发RowDataBound事件,而在这些行中TextBox是找不到的,把null隐性转换为TextBox就抛出一个异常~