有人可以建议动态地将HTML内容添加到ASP.NET页面的“正确”方法是什么?
我知道以下声明方法。
//Declaration
<%= MyMethodCall() %>
//And in the code behind.
protected String MyMethodCall()
{
return "Test Value";
}
是否有更好或最佳的练习方式?
编辑:我正在根据特定文件夹中的图片动态构建Galleriffic照片库。
答案 0 :(得分:27)
取决于你想做什么。
对于控件/文本,我通常使用LiteralControl
并将Text
属性设置为我要添加的HTML,然后可以将此控件添加到您希望其显示的页面上的任何位置< / p>
LiteralControl引用是 here
好的,因为你想要它用于Galleriffic,我想它会伪似出现......
LiteralControl imageGallery = new LiteralControl();
string divStart = @"<div id='thumbs'><ul class='thumbs noscript'>";
imageGallery.Text += divStart;
foreach ([image in images])
{
string imageHTML = @"<li><a class='thumb' name='optionalCustomIdentifier' ref='path/to/slide' title='your image title'>
<img src='path/to/thumbnail' alt='your image title again for graceful degradation' /></a>
<div class='caption'>[caption]<div></li>";
imageGallery.Text += imageHTML;
}
string divEnd = @"</ul></div>";
imageGallery.Text += divEnd;
this.[divOnPage].Controls.Add(imageGallery);
答案 1 :(得分:9)
Aspx:
<div id="DIV1" runat="server"></div>
代码背后:
DIV1.InnerHtml = "some text";
答案 2 :(得分:6)
有几种方法可以实现,使用方法实际上取决于您的方案和偏好。
答案 3 :(得分:0)
另一个选择
//.aspx
<asp:Literal ID="myText" runat="server"></asp:Literal>
//.aspx.cs
protected Literal myText;
myText.Text = "Hello, World!";