gwt
gwt

Reputation: 2413

How to create img tags in c# code behind inside a div according to the div class?

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

Answers (5)

patel.milanb
patel.milanb

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

akhil
akhil

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

Christian Phillips
Christian Phillips

Reputation: 18769

You could look at the TagBuilder class, this will assist in building what you need

http://www.asp.net/mvc/tutorials/older-versions/views/using-the-tagbuilder-class-to-build-html-helpers-cs

I'm not 100% sure this can be used with WebForms though, I have only used in MVC.

Upvotes: 0

Micah
Micah

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

KV Prajapati
KV Prajapati

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

Related Questions