日期:2014-05-18  浏览次数:20503 次

GridView与repeater性能的比较
我以前一直认为repeater性能最好,因为是使用SQL语句选择要使用的数据:select   *   from   table   where   ...进行分页,     但repeater   有个很不好的缺点就是无法自动分页.如果分页要写一大堆的代码.
        所以我现在想用GridView做,想使用其自动分页功能.但考虑如果使用自动分页,那么他的DataSource的SQL语句就会是:select   *   from   table;这样等于从SQL数据库中把所有的数据提取出来,然后再GridView来取得自己需要使用的数据,我想这样会不会太影响性能,会不会比repeater多十几倍的性能成本?所以现在使用起来很顾忌.
        请问高手们如何看待这个问题.

------解决方案--------------------
LZ 你这个比较的起点,选错了, GridView 你也可以不用他自带的分页功能,像 Repeater 一样使用自己的分页功能

性能上来说,主要在控件呈现出来, 由于内置许多功能,GridView 比较臃肿,相对来比 Repeater 要低,但是这一点上,通常又不是那么的明显,
因为你不会用 GridView 或者 Repeater 或者 DataList 来一次性显示 几百上千条数据吧?
这样的数据,就是用 Response.Write 也慢啊,仅仅是呈现成 html 的问题

你要提升性能,当然是分页取数了, 或者考虑缓存,控件本身性能提升不明显
------解决方案--------------------
如果只呈现数据不用控件就行了,
------解决方案--------------------
// 自定义 PagerTemplate

<%@ Page Language= "C# " %>
<%@ Import Namespace= "System.Data " %>

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN " "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd ">

<script runat= "server ">

protected void Page_Load(object sender, EventArgs e)
{
if (!IsPostBack) {
LoadProductData(GridView1);
LoadProductData(GridView2);
}
}

void LoadProductData(GridView grid)
{
DataTable dt = CreateSampleData();

grid.DataSource = dt;
grid.DataBind();
}

#region sample data

static DataTable CreateSampleData()
{
DataTable tbl = new DataTable( "Items ");

tbl.Columns.Add( "ItemID ", typeof(int));
tbl.Columns.Add( "ItemName ", typeof(string));

int count = 0;
while (count++ < 100) {
tbl.Rows.Add(count, "Item# " + count.ToString( "d3 "));
}

return tbl;
}

#endregion


protected void GridView_PageIndexChanging(object sender, GridViewPageEventArgs e)
{
GridView theGrid = sender as GridView; // refer to the GridView
int newPageIndex = 0;

if (0 > e.NewPageIndex) { // when click the "GO " Button
TextBox txtNewPageIndex = null;
TextBox txtNewPageSize = null;
GridViewRow pagerRow = theGrid.BottomPagerRow; // refer to PagerGroup

//
if (null != pagerRow) {
txtNewPageIndex = pagerRow.FindControl( "txtNewPageIndex ") as TextBox; // refer to the TextBox with the NewPageIndex value
txtNewPageSize = pagerRow.FindControl( "txtNewPageSize ") as TextBox;
}

//
if (null != txtNewPageIndex) {
newPageIndex = int.Parse(txtNewPageIndex.Text) - 1; // get the NewPageIndex
}
//
if (null != txtNewPageSize) {
int newPageSize = int.Parse(txtNewPageSize.Text);
theGrid.PageSize = 0 > newPageSize ? 10 : newPageSize;
}

// check to prevent form the NewPageIndex out of the range
newPageIndex = newPageIndex < 0 ? 0 : newPageIndex;