创建 ASP.Net 表非常慢,有更好的解决方案吗?

发布于 2024-08-22 15:40:50 字数 1674 浏览 3 评论 0原文

我有一个包含 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);

此代码运行速度非常慢,有没有办法加快速度?

I have a DataTable with 20.000 row and 15 column. I need to create an ASP.Net Table, my code is similar to this:

//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);

this code works really slow, is there a way to hasten this ?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

橪书 2024-08-29 15:40:50

您可以使用 Repeater 并将 和必要的 放入 ItemTemplate 中。

首先检查是否真的是表的构建占用了很多时间。我的猜测是,对于具有 20,000 行和 15 列的表格,HTML 代码的传输和呈现导致速度变慢。

You could use a Repeater and put a <tr> with the necessary <td>'s in the ItemTemplate.

First check if it really is the building of the table that is taking up a lot of time. My guess is that it is the transferring and rendering of the HTML-code for a table with 20.000 rows and 15 columns that is slowing things down here.

煮茶煮酒煮时光 2024-08-29 15:40:50

我建议使用数据绑定方法,您实际上使用数据绑定控件来绑定 DataTable,例如 GridView中继器

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

I'd suggest using a databinding approach, where you actually bind your DataTable using a databound-control, such as a GridView or Repeater.

With these controls, you should be making use of the DataSource property and DataBind methods. You don't need to manually add rows to a table etc. Databinding takes care of these fundamentals..

忆离笙 2024-08-29 15:40:50

我想到了两个想法:

  • 不要检索自您以来的所有 20K 行
    无论如何都无法显示全部。如果
    您需要过滤,您可以返回
    一旦你知道用户是什么,数据库
    过滤依据。

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

Two thoughts come to mind:

  • Don't retrieve all 20K rows since you
    can't show all of them anyway. If
    you need filtering you can go back to
    the DB once you know what the user is
    filtering by.

  • Modify the datareaders SQL to have
    the fields + the other items needed
    such as the count and empty field and
    then load the datareader into the
    datatable (datatable.load)

污味仙女 2024-08-29 15:40:50

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

You might get better performance declaring the entire table in a string of HTML with a single Literal or LiteralControl, and/or putting the declared HTML into it's own user control or reuse. Won't be as flexible but you won't be creating nearly as many objects.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文