尝试设置控件可见性时无效的强制转换异常

时间:2014-10-04 10:32:45

标签: c# asp.net gridview

我在ItemTemplate内的GridView内有此控件:

<img alt="" style="cursor: pointer" src="images/plus.png" runat="server" 
     id="img_expand1" />

我需要在条件中将可见性设置为false:

 else
 {
    Image img = (Image)e.Row.FindControl("img_expand1");
    img.Visible = false;
 }

但是我收到了一个错误:

Invalid cast exception. Impossible to cast object type
'System.Web.UI.HtmlControls.HtmlImage' on type
'System.Web.UI.WebControls.Image'.

我做错了什么?我该如何解决这个问题?

1 个答案:

答案 0 :(得分:1)

System.Web.UI.HtmlControls.HtmlImage img = (System.Web.UI.HtmlControls.HtmlImage)e.Row.FindControl("img_expand1");
img.Visible = false;