C# 如何解决用户代码未处理的InvalidCastException?
我使用了两个图像按钮来编辑和删除第一页,当我点击下一页时,我得到了一个错误C# 如何解决用户代码未处理的InvalidCastException?,c#,asp.net,C#,Asp.net,我使用了两个图像按钮来编辑和删除第一页,当我点击下一页时,我得到了一个错误 GridViewRow row=((GridViewRow )((ImageButton )e.CommandSource ).NamingContainer ); ImageButton imgdelete = (ImageButton)row.FindControl("imgdelete"); string pid = e.CommandArgument.ToString(); if (e.CommandName
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'
您正试图将代码行中的
System.Web.UI.WebControls.GridView
强制转换为System.Web.UI.WebControls.ImageButton
,并使用括号(
放错位置
GridViewRow row= (GridViewRow)(((ImageButton )e.CommandSource).NamingContainer);
mageButton imgdelete = (ImageButton)row.FindControl("imgdelete");
试试这个
GridViewRow row=((GridViewRow )((ImageButton )e.CommandSource ).NamingContainer );
不要试图将
GridView
投射到ImageButton
?你指的是括号。请避免在没有文字解释的情况下发布答案。只有几个字总比没有好。也不需要写文章。查看我的编辑:)
GridViewRow row = (GridViewRow) (((ImageButton)e.CommandSource).NamingContainer);