当前位置: 代码迷 >> ASP.NET >> 帮忙解决一下啊无法将类型为“System.Web.UI.LiteralControl”的对象强制转换为类型“System.Web.UI.WebControls.TextBox”。解决办法
  详细解决方案

帮忙解决一下啊无法将类型为“System.Web.UI.LiteralControl”的对象强制转换为类型“System.Web.UI.WebControls.TextBox”。解决办法

热度:4978   发布时间:2013-02-25 00:00:00.0
帮忙解决一下啊啊。。无法将类型为“System.Web.UI.LiteralControl”的对象强制转换为类型“System.Web.UI.WebControls.TextBox”。
//更新 
  protected void GridView1_RowUpdating(object sender, GridViewUpdateEventArgs e)
  {
  SqlConnection conn = new SqlConnection(ConfigurationManager.AppSettings["strconnection"]);
  string sqlstr = "update tuser set ucoding='"
  + ((TextBox)(GridView1.Rows[e.RowIndex].Cells[1].Controls[0])).Text.ToString().Trim() + "',uarea='"
  + ((TextBox)(GridView1.Rows[e.RowIndex].Cells[2].Controls[0])).Text.ToString().Trim() + "',tname='"
  + ((TextBox)(GridView1.Rows[e.RowIndex].Cells[3].Controls[0])).Text.ToString().Trim() + "' where id='"
  + GridView1.DataKeys[e.RowIndex].Value.ToString() + "'";
  sqlcom = new SqlCommand(sqlstr, conn);
  conn.Open();
  sqlcom.ExecuteNonQuery();
  conn.Close();
  GridView1.EditIndex = -1;
  bind();
  }

------解决方案--------------------------------------------------------
去掉里面的(TextBox),那个很多余啊。
protected void GridView1_RowUpdating(object sender, GridViewUpdateEventArgs e)
{
SqlConnection conn = new SqlConnection(ConfigurationManager.AppSettings["strconnection"]);
string sqlstr = "update tuser set ucoding='"
+ GridView1.Rows[e.RowIndex].Cells[1].Controls[0].ToString().Trim() + "',uarea='"
+ GridView1.Rows[e.RowIndex].Cells[2].Controls[0].ToString().Trim() + "',tname='"
+ GridView1.Rows[e.RowIndex].Cells[3].Controls[0].ToString().Trim() + "' where id='"
+ GridView1.DataKeys[e.RowIndex].Value.ToString() + "'";
sqlcom = new SqlCommand(sqlstr, conn);
conn.Open();
sqlcom.ExecuteNonQuery();
conn.Close();
GridView1.EditIndex = -1;
bind();
}
------解决方案--------------------------------------------------------
建议楼主再检查以下Cells单元格数对了没有,索引号从0开始计;
之前碰到类似的问题


---------------------------------------
“苏州软件(微软技术)实训基地”[color=#FF0000][/color]由苏州工业园区政府和微软(中国)有限公司共同建立,由苏州高博软件培训中心承办。基地以苏州工业园区软件人才培养和服务外包基地建设需求为导向,充分利用微软的软件技术、产业教育和项目管理经验,帮助学员系统掌握微软技术、真实体验项目角色、全面提升职场竞争力。 

公司网址[color=#FF0000][/color]:http://www.gemsoftware.com.cn 

------解决方案--------------------------------------------------------
where id='"
+ GridView1.DataKeys[e.RowIndex].Value.ToString() + "'"; 

这里面的单引号去掉:

where id="
+ GridView1.DataKeys[e.RowIndex].Value.ToString() + "";
------解决方案--------------------------------------------------------
将那一列转换为模板列,用FindControl找到
------解决方案--------------------------------------------------------
去掉也不行,你的“'370200-00000015”是不可能转换成int型的。
除非你把表的id字段改成其他类型。
  相关解决方案