日期:2014-05-18  浏览次数:20893 次

无法将类型为“System.Web.UI.LiteralControl”的对象强制转换为类型“System.Web
protected void GridView1_RowCreated(object sender, GridViewRowEventArgs e)
  {
  if (e.Row.RowType == DataControlRowType.DataRow)
  {
  LinkButton lb = (LinkButton)e.Row.Cells[7].Controls[2];//这里出错
  lb.Attributes.Add("onclick", "javascript:return delval()");//做删除
  e.Row.Cells[7].Controls.RemoveAt(2);
  e.Row.Cells[7].Controls.AddAt(2, lb);
  }
  }

------解决方案--------------------
把HTML也弄出来看看,
------解决方案--------------------
LinkButton lb = (LinkButton)e.Row.FindControl("LinkButton1");
------解决方案--------------------
错误是说
你的e.Row.Cells[7].Controls[2]得到的是LiteralControl不是LinkButton
------解决方案--------------------
你的错误是
Controls[2]不是LinkButton 这个类型
你看看你的程序里面Controls[2]中是什么类型