[英]Dynamically add HTML to ASP.NET page
有人可以建议动态地将HTML内容添加到ASP.NET页面的“正确”方法是什么?
我知道以下声明方法。
//Declaration
<%= MyMethodCall() %>
//And in the code behind.
protected String MyMethodCall()
{
return "Test Value";
}
有更好或最好的练习方式吗?
编辑:我正在根据位于特定文件夹中的图像动态构建Galleriffic照片库。
取决于你想做什么。
对于控件/文本,我通常使用LiteralControl
并将Text
属性设置为我想要添加的HTML,然后可以将此控件添加到您希望它出现的页面上的任何位置
LiteralControl参考在这里
好吧,因为你想要它为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);
Aspx:
<div id="DIV1" runat="server"></div>
代码背后:
DIV1.InnerHtml = "some text";
有几种方法可以实现,实际使用取决于您的方案和偏好。
另外一个选项
//.aspx
<asp:Literal ID="myText" runat="server"></asp:Literal>
//.aspx.cs
protected Literal myText;
myText.Text = "Hello, World!";
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.