3

I would like to add <asp:image> from my codebehind onto the page. The user on my website will upload X number of pictures, and i don't want to add the <asp:images> before the picture is uploaded.

The reason i want to add the <asp:image> is to show the user the picture which has been uploaded.

How can this be done?

I've seen the placeholder controller being used, but how can that be used when the variables gotta be different? For each upload the image should be added onto the site.

1 Answer 1

3

You can do that by creating a new Image object variable and then assigning its preperties properly then attaching the image to an existing object on your page.

Image myImg = new Image();
myImg.ImageUrl = "path_of_the_image.jpg";
myImg.Visible = true;
Panel1.Controls.Add(myImg);   //You can attach the image to any control on your page

Or similarly:

this.Controls.Add(myImg);     //Incase you wanted the image on your page controls
Label1.Controls.Add(myImg);   //Incase you wanted the image to appear in a certain label

However, this object will not live beyond the life of the code you execute. Meaning, you won't be able to interact with this object later on, since it is defined after the scope of Page_Init. If you want to add this image dynamically then be able to interact with it, you should add it during the Page_Init event.

Sign up to request clarification or add additional context in comments.

5 Comments

I should probably mention this is for C# webforms
Will I be able to add several pictures? It seems like the controller is being overwritten when I add picture nr 2.
You can add as many controls as you want. Nothing will be overwritten. You are simply ADDING to the existing list of controls.
You can add controls to a PlaceHolder too. a Panel is not the same as PlaceHolder, but all controls have a built in Controls array that you can add more controls to.
Your syntax wasn't valid; Fixed that for you as it's really not the issue here.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.