我想知道这两者之间有什么区别,当我在HtmlHelper中构建我的表时,对它们中的一个或另一个有什么好处
HtmlTable table = new HtmlTable();和:
TagBuilder table = new TagBuilder("table");这个问题跟这个问题差不多,
Why use TagBuilder instead of StringBuilder?
但我更想知道这两者之间的区别。
发布于 2010-06-15 17:21:51
主要区别在于,HtmlTable为<table>元素的所有有效HTML (例如Width、Height、CellSpacing等)提供了类型化且命名正确的属性。它还具有Rows属性,该属性是HtmlTableRow对象的类型化集合,每个对象都返回一个<tr>元素。
TagBuilder是一个更通用的<table>,它当然可以用来构造HTML,但你需要做更多的工作,以一种不太安全的类型和不容易阅读的方式。
HmlTable以TagBuilder无法提供的方式提供帮助的一个具体示例是在<table>元素上设置width=""属性。
使用HtmlTable
HtmlTable htmlTable = new HtmlTable();
htmlTable.Width = "100px";使用TagBuilder
TagBuilder tagBuilder = new TagBuilder("table");
tagBuilder.Attributes["width"] = "100px";请注意,对于TagBuilder,元素的名称table和属性的名称width都是字符串,它们引入了两个在使用HtmlTable时不会发生的错误(拼写错误)机会。
https://stackoverflow.com/questions/3043800
复制相似问题