在 ASP.NET 中创建表的其他选项
我正在后面的代码中使用 StringBuilder 创建一个巨大的 html 表,基于用户选择的各种搜索条件。逻辑很复杂,因为我必须创建子标题、嵌套表格等,而且很难维护或修改。有没有更好的方法来处理此类问题?
谢谢!!
I am creating a huge html table using StringBuilder in the code behind, based on various search criterias selected by the user. The logic is complex as I have to create sub heading, nested tables etc. and it is really hard to maintain or modify. Is there a better way to deal with such kind of problems?
Thanks!!
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
所有 ASP 和 Html 控件都封装在类中。您对
表<感兴趣/code>
类(对于 ASP)和
HtmlTable
用于更轻量级的纯 html 类。如果我必须选择,我会选择 html 版本,除非您想将服务器端事件添加到表中。
All ASP and Html controls are encapsulated in classes. You're interested in the
Table
class (for ASP) andHtmlTable
for the more light-weight, html-only class.If I had to choose, I'd go for the html one, unless you want to add server-side events to the table.
几年前我也遇到过同样的情况。问题包括动态栏、副标题、单元格,一切。典型的表格大约有 1000 行和 50 列(即 50,000 个单元格!)。最初的实现使用了 GridView 并且执行得很糟糕。我重写了视图以使用
Repeater
,这是一个非常轻量级的循环控件,带有Literal
控件。与 100% StringBuilder 相比,这有点疯狂。我将其与一堆返回标准 html 位的字符串表示形式的静态方法结合起来(有点像 ASP.NET MVC 的“html 助手”),并保持对象模型完全隔离。一切都非常快(我忘记了,但我认为Repeater
和Literal
控件的呈现方式是直接响应流的,因此性能与StringBuilder,也许更好)。
即使上述内容也会很复杂,并且类似于您自己的方法。但保持理智的关键是保持不同部分的独立(对象模型、html 生成和动态绑定)。这几乎就像构建您自己的临时框架一样。但对于像这样严肃的工作,您需要在仅限于网络浏览器的情况下了解具体情况。
A few years ago I was in the same situation. The problem included dynamic columns, subheadings, cells, everything. A typical table would be around 1000 rows and 50 columns (that's 50,000 cells!). The original implementation used a
GridView
and performed horribly. I rewrote the view to use aRepeater
, a very light-weight looping control, withLiteral
controls. That reigned in a bit of the madness vs. 100%StringBuilder
. I combined that with a bunch of static methods which returned string representations for standard html bits (kind of like ASP.NET MVC's "html helpers"), as well as keeping the object model completely isolated. It was all very fast (I forget, but I think the way theRepeater
andLiteral
controls are rendered is directly to the response stream, so performance was comparable toStringBuilder
, perhaps even better).Even the above will be complex, and is akin to your own approach. But the key to maintaining sanity is to keep the different pieces separate (object model, html generation, and dynamic binding). It's almost like building your own ad-hoc framework. But for serious jobs like this, you need to get nitty-gritty when confined to web browsers.
始终有内置的本机 ASP.NET Table 控件
http://www.w3schools.com/aspnet/ control_table.asp
There's always the built in native ASP.NET Table control
http://www.w3schools.com/aspnet/control_table.asp
GridView,ListView,数据列表,表 e&。
GridView, ListView, DataList, Table e&.