【问题标题】:How to Resolve InvalidCastException was unhandled by User code?如何解决用户代码未处理 InvalidCastException?
【发布时间】:2013-12-09 19:32:46
【问题描述】:
GridViewRow row=((GridViewRow )((ImageButton )e.CommandSource ).NamingContainer );
ImageButton imgdelete = (ImageButton)row.FindControl("imgdelete");

string pid = e.CommandArgument.ToString();
if (e.CommandName == "Edit"){
    Response.Redirect("PatientRegistration.aspx?pdid=" + pid);
}
if (e.CommandName == "Delete"){
    BL_Property.PatientID = pid;
    BL_Patient.DeletePatient(BL_Property);
    ShowAllPatient();
}

我使用两个图像按钮进行编辑和删除当我点击下一页时我得到了正确的第一页我得到了一个错误

InvalidCastException was unhandled by User code
Error Message :- Unable to cast object of type 'System.Web.UI.WebControls.GridView' to type 'System.Web.UI.WebControls.ImageButton'

【问题讨论】:

  • 不要尝试将GridView 转换为ImageButton

标签: c# asp.net


【解决方案1】:

您正尝试将代码行中的 System.Web.UI.WebControls.GridView 转换为 System.Web.UI.WebControls.ImageButton,括号 ( 放错了位置,

GridViewRow row=((GridViewRow )((ImageButton )e.CommandSource ).NamingContainer );

试试这个,

GridViewRow row = (GridViewRow)  (((ImageButton)e.CommandSource).NamingContainer);

【讨论】:

  • 你的意思是括号。
【解决方案2】:

试试下面的代码:

GridViewRow row= (GridViewRow)(((ImageButton )e.CommandSource).NamingContainer);
mageButton imgdelete = (ImageButton)row.FindControl("imgdelete");

【讨论】:

  • 请避免发布没有文字说明的答案。寥寥数语总比没有好。也不需要写论文。看看我的编辑。 :)
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2012-04-18
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2013-12-28
  • 2014-10-18
相关资源
最近更新 更多