[英]Proper way to output HTML to the page
我意識到這可能是我應該知道的基本知識,但是我正在自學C#和asp.net,因此我有點迷失了。
我有一個存儲過程,它將返回約700-800個圖像URL。
我需要為所有800個圖像URL構建這樣的HTML,然后將HTML返回頁面:
<div class="tile">
<img src="source.png" height="100" width="100" />
</div>
這是我目前的代碼:
if (reader.HasRows)
{
string flagwallcontent = ""; //using a string to build html
while (reader.Read())
{
flagwallcontent = flagwallcontent + "<div class='tile'>";
flagwallcontent = flagwallcontent + "<img src='" + reader.GetString(0) + "' height='100' width='100'/>";
flagwallcontent = flagwallcontent + "</div>";
}
FlagWallLiteral.Text = flagwallcontent; //returning html to asp literal
}
我覺得這不是有效的方法。 使用字符串構建HTML並將HTML返回為asp文字。 您能建議最好的方法是什么嗎?
一種可能的方法是將ASP.NET Panel
(呈現為<div>
)作為要添加的X百個圖像的容器,如下所示:
if (reader.HasRows)
{
while (reader.Read())
{
// Create new image control
var newImage = new Image ();
newImage.ImageUrl = reader.GetString(0);
newImage.Height = 100;
newImage.Width = 100;
// Add new image to panel
Panel1.Controls.Add (newImage);
}
}
這允許ASP.NET在使用強類型C#API時呈現HTML。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.