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

时间:2010-02-22 14:45:21

标签: c# asp.net algorithm rendering performance

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

这段代码工作得很慢,有没有办法加快这个?

4 个答案:

答案 0 :(得分:7)

您可以使用Repeater并在ItemTemplate中添加<tr>必需的<td>

首先检查表格的构建是否占用了大量时间。我的猜测是,对于一个包含20.000行和15列的表的HTML代码进行传输和呈现会降低这里的速度。

答案 1 :(得分:2)

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

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

答案 2 :(得分:2)

想到两个想法:

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

  • 修改要拥有的datareaders SQL 字段+所需的其他项目 例如计数和空字段和 然后将datareader加载到 datatable(datatable.load)

答案 3 :(得分:0)

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

相关问题