简体   繁体   中英

How to hide an asp:Image contained within a Repeater if the image field in the database is set to null

I've got an asp:Image control within an asp:Repeater. The image is displayed through an image handler which retrieves the image from a database (BLOB), and uses query strings to choose which image is displayed.

This method works perfectly, however when an image is not displayed, a red cross appears in Internet Explorer. How exactly would I go about hiding the asp:Image when it is not available, considering the repeater architecture?

<asp:Image ID="image" runat="server" ImageUrl='<%# "~/Handler.ashx?id=" + Eval("ID")%>'/>

It's been a while since I've done webforms, but if I remember correctly you need to do this in the ItemDataBound callback.

In the callback you can do something along the lines of the following code. Beware it is untested, just off the top of my head, but should get you on the right track.

RepeaterItem ri = e.Item;

var image = ri.FindControl("image") as Image;
if (image != null){
    bool isUrlValid = ....;// your check here
    image.Visible = isUrlValid
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM