繁体   English   中英

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

[英]Creating an ASP.Net Table is very slow, is there a better solution?

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

这段代码真的很慢,有没有办法加快速度?

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

首先检查是否真的是在花费大量时间构建桌子。 我的猜测是,对于具有20.000行和15列的表,HTML代码的传输和呈现会降低速度。

我建议使用数据绑定方法,在该方法中,您实际上是使用DataTable绑定控件(例如GridViewRepeater)来绑定DataTable

使用这些控件,您应该利用DataSource属性和DataBind方法。 您不需要手动向表等添加行。数据绑定会照顾这些基础。

我想到两个想法:

  • 不要检索所有2万行,因为无论如何您都无法显示全部。 如果您需要过滤,则可以在知道用户要过滤的内容后返回数据库。

  • 修改数据读取器SQL,使其具有字段+所需的其他项(例如count和empty字段),然后将数据读取器加载到数据表(datatable.load)中

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

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM