Reputation: 2413
I have the following div tag :
<div class="slideshow"></div>
I would like to create several img tags inside that div in c# codebehind as follows :
<div class="slideshow">
<img src="images/image1.png" alt="" width="600" height="300" />
<img src="images/image2.png" alt="" width="600" height="300" />
<img src="images/image3.png" alt="" width="600" height="300" />
</div>
How can i do this ?
Upvotes: 1
Views: 14172
Reputation: 5992
using For loop inside the codebehind // this will create 10 image tag for you like you asked
For(int i=0; i < 10; i++)
{
myPanel.Controls.Add( new HtmlImage() //OR you can have ServerSide Control eg: new Image ()
{
Src = "Image/Image" + i + ".png",
Alt = "my image" + i,
Width = 600,
Height = 300
});
}
Upvotes: 1
Reputation: 1212
have a look at http://learning2code.net/Learn/2009/8/12/Adding-Controls-to-an-ASPNET-form-Dynamically.aspx
Convert html tag to server control.
<div id="divid" runat="server"></div>
and in code behing you can use
Image img = new Image();
divid.Controls.Add(img);
Upvotes: 1
Reputation: 18769
You could look at the TagBuilder class, this will assist in building what you need
I'm not 100% sure this can be used with WebForms though, I have only used in MVC.
Upvotes: 0
Reputation: 10395
Have you looked into using the <asp:Image />
tag?
http://msdn.microsoft.com/en-us/library/system.web.ui.webcontrols.image.aspx
Upvotes: 0
Reputation: 94645
Try to convert html
tag to Htmlserver
control and also specify the id
attribute.
<div class="slideshow" runat="server" id="slideshow"></div>
Code-behind:
slideshow.Controls.Add(new HtmlImage()
{
Src="",
Alt="",
Width=200,
Height=200
});
Upvotes: 5