无法将类型为“System.Web.UI.DataBoundLiteralControl”的对象强制转换为类型“System.Web.UI.WebControls.TextBox”。 
说明: 执行当前 Web 请求期间,出现未处理的异常。请检查堆栈跟踪信息,以了解有关该错误以及代码中导致错误的出处的详细信息。 异常详细信息: System.InvalidCastException: 无法将类型为“System.Web.UI.DataBoundLiteralControl”的对象强制转换为类型“System.Web.UI.WebControls.TextBox”。
代码如下:这是那里错啊
SqlConnection con = new SqlConnection("server=.;Initial Catalog=webdata;Integrated Security=True");
        con.Open();
        string sqlstr = "update userlist set username='"
            + ((TextBox)(GridView2.Rows[e.RowIndex].Cells[1].Controls[0])).Text.ToString().Trim() + "',pwd='"
            + ((TextBox)(GridView2.Rows[e.RowIndex].Cells[2].Controls[0])).Text.ToString().Trim() + "' where id='"
            + GridView2.DataKeys[e.RowIndex].Value.ToString() + "'";
        SqlCommand cmd = new SqlCommand(sqlstr, con);
        cmd.ExecuteNonQuery();
        con.Close();
        GridView2.EditIndex = -1;
        this.databinder();

解决方案 »

  1.   

    Controls[0]改为Controls[1]试试
    或者用FindControl方法找到TextBox
      

  2.   

    同1楼,用Cells[1].FindContro 方法找吧
    或者你调试到那步看一个Cell里第几个你要的控件。
      

  3.   


    代码如下:这是那里错啊 
    SqlConnection con = new SqlConnection("server=.;Initial Catalog=webdata;Integrated Security=True"); 
            con.Open(); 
            string sqlstr = "update userlist set username='" 
                + Convert.ToString((TextBox)(GridView2.Rows[e.RowIndex].Cells[1].Controls[0])).Text.ToString().Trim() + "',pwd='" 
                + Convert.ToString((TextBox)(GridView2.Rows[e.RowIndex].Cells[2].Controls[0])).Text.ToString().Trim() + "' where id='"
     
                + GridView2.DataKeys[e.RowIndex].Value.ToString() + "'"; 
            SqlCommand cmd = new SqlCommand(sqlstr, con); 
            cmd.ExecuteNonQuery(); 
            con.Close(); 
            GridView2.EditIndex = -1; 
            this.databinder();
      

  4.   

    楼主最好用FindControl方法来找出TextBox控件
      

  5.   

    楼主这样试一下看行不行:TextBox tb1 = (TextBox)GridView2.Rows[e.RowIndex].FindControl("控件标识1");
            TextBox tb2 = (TextBox)GridView2.Rows[e.RowIndex].FindControl("控件标识2");
            SqlConnection con = new SqlConnection("server=.;Initial Catalog=webdata;Integrated Security=True");
            con.Open();
            string sqlstr = "update userlist set username='"
                + tb1.Text.Trim() + "',pwd='"
                + tb2.Text.Trim() + "' where id='"
                + GridView2.DataKeys[e.RowIndex].Value.ToString() + "'";
            SqlCommand cmd = new SqlCommand(sqlstr, con);
            cmd.ExecuteNonQuery();
            con.Close();
            GridView2.EditIndex = -1;
            this.databinder();