2012-07-11 81 views
1

我有以下div標籤:如何根據div類在div內部的c#代碼中創建img標籤?

<div class="slideshow"></div> 

我想在C#代碼隱藏創建DIV中幾個的img標籤如下:

<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> 

我怎麼能這樣做呢?

+0

@akhil我有不知道該怎麼做! – Karamafrooz 2012-07-11 11:22:40

+0

你使用ASP.NET web窗體還是ASP.NET MVC?你需要創建固定數量的那些img或未知的動態計算數字嗎?你需要指定更多的細節 – 2012-07-11 11:25:36

+0

@Karamafrooz ok看看我的asnwer它可能會幫助你 – akhil 2012-07-11 11:28:09

回答

5

嘗試將html標記轉換爲Htmlserver控件並指定id屬性。

<div class="slideshow" runat="server" id="slideshow"></div> 

代碼隱藏:

slideshow.Controls.Add(new HtmlImage() 
     { 
       Src="", 
       Alt="", 
       Width=200, 
       Height=200 
     }); 
+0

非常感謝你!它解決了我的問題!很好,很簡單的代碼;) – Karamafrooz 2012-07-11 11:38:52

1

//這將創建10個圖像廣告代碼,你喜歡,你問

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 
       }); 
} 
+0

非常感謝你! – Karamafrooz 2012-07-11 11:38:30