meeheecaan
meeheecaan

Reputation: 11

In ASP.NET, how can I get an image from the code behind and display it in a <div> section?

Boss just gave me a webpage to work with, and I've never done webpages before. When I got it there was an image I need to replace

<div>
    <!--<img style="padding-top:5px;" class="featured" src="path/name.jpg" />-->
    html text

I had to go in to the .cs file of the .aspx file and a path to the image

Image image = new Image();
image.ImageUrl = path;

and then back where the old image was

<div>
    <asp:Image style="padding-top:5px;" class="featured" runat="server" ID="image" />
    html text

But I'm not sure how to get the new image to display correctly where the old one was, since I've never worked with asp files before. Any suggestions on what to do?

Upvotes: 0

Views: 11929

Answers (3)

Zeek2
Zeek2

Reputation: 424

Similar but slightly different:

Mark-up

<img id="Image1" alt="image" runat="server" />

In code behind (VB or C#):

Image1.src = "Images/Butterfly.jpg"

Upvotes: 0

Izzy
Izzy

Reputation: 6866

As mentioned in my comment you can access the control directy by using it's Id in the C# code or you can provide the ImageUrl in your aspx page.

Providing the ImageUrl in your aspx

<asp:Image runat="server" ID="image" ImageUrl="../Path/SomeImage.png" />

Or if you want to use C#

image.ImageUrl = "../Path/SomeImage.png";

Upvotes: 0

swilliams
swilliams

Reputation: 206

In the asp code, be certain to give the image element an ID:

<asp:image id="setincode" width="250" runat="server" />

In the code-behind, retrieve the control by the ID, then you can set the url:

Image img = (Image)FindControl("setincode");
img.ImageUrl = "Images/Butterfly.jpg";

Upvotes: 1

Related Questions