Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Adding <asp:image> from codebehind

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.

like image 373
sindrem Avatar asked Dec 20 '25 16:12

sindrem


1 Answers

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.

like image 53
Ahmad Avatar answered Dec 23 '25 05:12

Ahmad



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!