无法将类型为“System.Web.UI.WebControls.CheckBox”的对象强制转换为类型“System.Web.UI.WebControls.TextBox”。
代码如下: string YHID = ((TextBox)(GridView1.Rows[e.RowIndex].Cells[2].Controls[0])).Text.ToString().Trim();
请高手指点一下,错在那里,怎么改。。谢谢! 

解决方案 »

  1.   

     CheckBox cbox = (CheckBox)GridView1.Rows[i].FindControl("CheckBox1");
                      if (cbox.Checked == true)
                      {
                          int id = int.Parse(GridView1.DataKeys[i].Value.ToString());
                          
                      }
      

  2.   

    GridView1.Rows[e.RowIndex].Cells[2].Controls[0])).Text.ToString().Trim()这句话返回的是一个checkbox类型,而你想要的类型是textbox,类型不匹配肯定报错啦,引用1楼的,应该可以解决
      

  3.   

    CheckBox cbox = GridView1.Rows[i].FindControl("CheckBox1") as CheckBox;
      if (cbox.Checked == true)
      {
      int id = int.Parse(GridView1.DataKeys[i].Value.ToString());
        
      }
      

  4.   

    GridView1.Rows[e.RowIndex].Cells[2].Controls[0]
    这里写得有点问题。 Cell上的下标或者是 Cells[2].Controls的下标错了。
    GridView1.Rows[e.RowIndex].Cells[2].Controls[1] 你试下看,第一个Control应该是一个Literal控件。