作者热门文章
- c - 在位数组中找到第一个零
- linux - Unix 显示有关匹配两种模式之一的文件的信息
- 正则表达式替换多个文件
- linux - 隐藏来自 xtrace 的命令
我有一个 Html 页面模板,我需要在 ASP.NET 中动态添加它的内容。我还需要制作许多模板页面实例,具体取决于数据。
很明显,使用字符串创建大型 Html 是非常肮脏的方式。所以我选择使用 HtmlGenericControl 生成我的 Html。我做到了。但是我无法从中获取生成的 Html 作为字符串。
在 ASP.NET 页面中添加这些控件并进行渲染很简单,但我需要它们的 Html。
如果不可能,是否有任何其他生成 Html 的结构化方法...???
最佳答案
@Bartdude 讲述的技巧非常有效......
对于其他人,解决方案是这样的......
// create you generic controls
HtmlGenericControl mainDiv = new HtmlGenericControl("div");
// setting required attributes and properties
// adding more generic controls to it
// finally, get the html when its ready
StringBuilder generatedHtml = new StringBuilder();
using (var htmlStringWriter = new StringWriter(generatedHtml))
{
using(var htmlTextWriter = new HtmlTextWriter(htmlStringWriter))
{
mainDiv.RenderControl(htmlTextWriter);
output = generatedHtml.ToString();
}
}
希望这对即将到来的读者有所帮助...:)
关于c# - 如何从 HtmlGenericControl 获取生成的 Html,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/16442497/
我是一名优秀的程序员,十分优秀!