只是想知道这两者之间的区别是什么,以及当我在 HtmlHelper 中构建表格时它们中的一个或另一个的好处是什么
HtmlTable table = new HtmlTable();
和:
TagBuilder table = new TagBuilder("table");
这和这个问题差不多,
Why use TagBuilder instead of StringBuilder?
但我更具体地想知道这两者之间的区别。
最佳答案
主要区别在于 HtmlTable
为 <table>
的所有有效 HTML 属性提供类型化和正确命名的属性元素(例如 Width
、 Height
、 CellSpacing
等)。它还具有 Rows
属性,它是 HtmlTableRow
的类型化集合每个对象都反对 <tr>
元素。
TagBuilder
是一个更通用的 API,当然可以用来构建 HTML <table>
但您需要以类型安全性较低且不易阅读的方式做更多工作。
一个具体的例子,其中 HmlTable
以一种方式提供帮助 TagBuilder
不在 width=""
的设置中<table>
上的属性元素。
与 HtmlTable
:
HtmlTable htmlTable = new HtmlTable();
htmlTable.Width = "100px";
与 TagBuilder
:
TagBuilder tagBuilder = new TagBuilder("table");
tagBuilder.Attributes["width"] = "100px";
请注意 TagBuilder
元素的名称,table
,以及属性的名称,width
, 是引入两个错误机会(拼写错误)的字符串,使用 HtmlTable
时不会发生这些错误(拼写错误) .
关于c# - HtmlTable 和 TagBuilder 的区别 ("table"),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/3043800/