简体   繁体   中英

How do i show image on my page depending on an IF-Statement

<asp:image ID="image" runat="server" hieght="200px" width="200px"></asp:image>

This is what is in my Code-Behind

string techItem = (string)Session["techItems"];

if (techItem == "1")
{
    lblName.Text = "Sony Playstation";

    Image myImage = new Image();
    myImage.ImageUrl("image-path");

}

This does not seem to work though.

You are creating a new image but not actually doing anything with it. You need to set the ImageUrl of the <asp:Image /> control that is on your page.

Change your for to;

if (techItem == "1")
{
    lblName.Text = "Sony Playstation";
    image.ImageUrl = "image-path"; // this is setting the image control's ImageUrl property

}

You may also what to show/hide the image depending on the state otherwise and image without an image src will sometimes show a broken image icon depending on the browser you are using.

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