我正在尝试使用ListView
. 对于每个项目,我必须创建这样的东西
<div>
<ul>
<li>foo<li>
<li>bar<li>
.... Dynamic count of <li>
<ul>
<span>Some dynamic text</span>
.. bunch of other dynamicly generated html
</div>
我的问题是生成 html 的更好方法是什么。通过使用这样的字符串连接
StringBuilder sb = new StringBuilder();
sb.Append("<div>");
.......
sb.Append("</div>")
或者像这样使用 HtmlGenericControl:
HtmlGenericControl htmlItem = new HtmlGenericControl( "div" );
....
using( TextWriter textWriter = new StringWriter( ) )
using( HtmlTextWriter htmlWriter = new HtmlTextWriter( textWriter ) )
{
HtmlGenericControl htmlItem = null;
CreateMenuItem( menuItem, 0, null );
htmlItem.RenderControl( htmlWriter );
return textWriter.ToString( );
}