【问题标题】:Creating an ASP.Net Table is very slow, is there a better solution?创建 ASP.Net Table 很慢,有没有更好的解决方案?
【发布时间】:2010-02-22 14:45:21
【问题描述】:

我有一个 20.000 行和 15 列的 DataTable。我需要创建一个 ASP.Net 表,我的代码是这样的:

//foreach Row in my DataTable do the following:


     TableRow t2Row = new TableRow();

                    TableCell t2Cell0 = new TableCell(); t2Cell0.Text = Count.ToString(); t2Row.Cells.Add(t2Cell0);
                    TableCell t2Cell1 = new TableCell(); t2Cell1.Text = dr["col1"].ToString(); t2Row.Cells.Add(t2Cell1);
                    TableCell t2Cell3 = new TableCell(); t2Cell3.Text = dr["col2"].ToString(); t2Row.Cells.Add(t2Cell3);
                    TableCell t2Cell2 = new TableCell(); t2Cell2.Text = dr["col3"].ToString(); t2Row.Cells.Add(t2Cell2);
                    TableCell t2Cell4 = new TableCell(); t2Cell4.Text = dr["col4"].ToString(); t2Row.Cells.Add(t2Cell4);
                    TableCell t2Cell5 = new TableCell(); t2Cell5.Text = ""; t2Row.Cells.Add(t2Cell5);
                    t2Cell5.ColumnSpan = 4;
                    TableCell t2Cell9 = new TableCell(); t2Cell9.Text = convertMinToTime(dr["col6"].ToString(), dr["col7"].ToString()); t2Row.Cells.Add(t2Cell9);
                    TableCell t2Cell10 = new TableCell(); t2Cell10.Text = dr["col8"].ToString(); t2Row.Cells.Add(t2Cell10);
                    TableCell t2Cell11 = new TableCell(); t2Cell11.Text = dr["col9"].ToString(); t2Row.Cells.Add(t2Cell11);
                    TableCell t2Cell12 = new TableCell(); t2Cell12.Text = dr["col10"].ToString(); t2Row.Cells.Add(t2Cell12);
                    TableCell t2Cell13 = new TableCell(); t2Cell13.Text = dr["col11"].ToString(); t2Row.Cells.Add(t2Cell13);

Table my_Table= new Table();
my_Table.Rows.Add(t2Row);

这段代码运行速度很慢,有没有办法加快这个速度?

【问题讨论】:

  • 哪个用户将滚动浏览 20K 行?

标签: c# asp.net algorithm rendering performance


【解决方案1】:

您可以使用中继器并将<tr> 与必要的<td> 放在ItemTemplate 中。

首先检查是否真的是表的构建占用了大量时间。我的猜测是,对于一个有 20.000 行和 15 列的表格,HTML 代码的传输和渲染正在减慢速度。

【讨论】:

  • 按照这些思路,确保表格具有“table-layout:fixed;” CSS 样式。 w3schools.com/Css/pr_tab_table-layout.asp 这大大提高了表格渲染速度。
  • 谢谢克里斯。还不知道那个!
  • 使用该样式会导致浏览器仅计算第一行渲染大小。其余行都使用这些值。这巧妙地回避了渲染表格所需的大量处理能力,这意味着它可以在接收到每一行时立即显示每一行,而不是等到整个表格加载后才知道它有多大。
【解决方案2】:

我建议使用数据绑定方法,您实际上使用数据绑定控件绑定您的DataTable,例如GridViewRepeater

使用这些控件,您应该使用DataSource 属性和DataBind 方法。您不需要手动向表中添加行等。数据绑定负责这些基础知识..

【讨论】:

    【解决方案3】:

    想到两个想法:

    • 不要检索所有 20K 行,因为您 无论如何都无法显示所有这些。如果 你需要过滤你可以回到 一旦你知道用户是什么,数据库 过滤依据。

    • 修改数据读取器 SQL 以具有 字段+所需的其他项目 例如计数和空字段和 然后将数据读取器加载到 数据表(datatable.load)

    【讨论】:

      【解决方案4】:

      使用单个 Literal 或 LiteralControl 在 HTML 字符串中声明整个表格和/或将声明的 HTML 放入它自己的用户控件或重用中可能会获得更好的性能。不会那么灵活,但您不会创建几乎一样多的对象。

      【讨论】:

        猜你喜欢
        • 2015-02-04
        • 2021-08-24
        • 2013-10-22
        • 1970-01-01
        • 2021-10-11
        • 1970-01-01
        • 1970-01-01
        • 2015-11-22
        • 2011-10-08
        相关资源
        最近更新 更多