读书人

GridView与repeater性能的比较解决方案

发布时间: 2012-01-03 22:16:07 作者: rapoo

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;
newPageIndex = newPageIndex > = theGrid.PageCount ? theGrid.PageCount - 1 : newPageIndex;


}
else { // when click the first, last, previous and next Button
newPageIndex = e.NewPageIndex;
}

// specify the NewPageIndex
theGrid.PageIndex = newPageIndex;

// rebind the control
LoadProductData(theGrid);
}

protected void drpNewPageIndex_SelectedIndexChanged(object sender, EventArgs e)
{
DropDownList drp = sender as DropDownList;
GridView grd = drp.NamingContainer.NamingContainer as GridView;
GridViewPageEventArgs pageArgs = new GridViewPageEventArgs(int.Parse(drp.SelectedValue));
GridView_PageIndexChanging(grd, pageArgs);
}

protected void GridView_DataBound(object sender, EventArgs e)
{
GridView grd = sender as GridView;
GridViewRow pagerRow = grd.BottomPagerRow;
DropDownList drp = pagerRow.FindControl( "drpNewPageIndex ") as DropDownList;
for (int i = 1; i <= grd.PageCount; i++) {
drp.Items.Add(new ListItem(i + "/ " + grd.PageCount, (i - 1).ToString()));
}
drp.SelectedIndex = grd.PageIndex;
}
</script>

<html xmlns= "http://www.w3.org/1999/xhtml " >
<head runat= "server ">
<title> ASP.NET DEMO16: GridViewPagerTemplate </title>
</head>
<body>
<form id= "form1 " runat= "server ">
<div>
<h3> Customing GridView 's Pager Template </h3>
<input type= "button " value= "Reload " onclick= "location.reload() " />
<asp:GridView ID= "GridView1 " runat= "server " AutoGenerateColumns= "true " AllowPaging= "true " OnPageIndexChanging= "GridView_PageIndexChanging " >
<PagerTemplate>
第 <%# (((GridView)Container.Parent.Parent).PageIndex + 1) %> / <%# ((GridView)Container.Parent.Parent).PageCount %> 页
每页 <asp:textbox id= "txtNewPageSize " runat= "server " width= "20px " text= ' <%# ((GridView)Container.Parent.Parent).PageSize %> ' /> 项
共 <%# ((DataTable)((GridView)Container.Parent.Parent).DataSource).Rows.Count %> 项
<asp:linkbutton id= "btnFirst " runat= "server " Enabled= ' <%# ((GridView)Container.Parent.Parent).PageIndex != 0 %> ' causesvalidation= "False " commandargument= "First " commandname= "Page " text= "首页 " />
<asp:linkbutton id= "btnPrev " runat= "server " Enabled= " <%# ((GridView)Container.Parent.Parent).PageIndex != 0 %> " causesvalidation= "False " commandargument= "Prev " commandname= "Page " text= "上一页 " />
<asp:linkbutton id= "btnNext " runat= "server " Enabled= " <%# ((GridView)Container.Parent.Parent).PageIndex+1 != ((GridView)Container.Parent.Parent).PageCount %> " causesvalidation= "False " commandargument= "Next " commandname= "Page " text= "下一页 " />
<asp:linkbutton id= "btnLast " runat= "server " Enabled= " <%# ((GridView)Container.Parent.Parent).PageIndex+1 != ((GridView)Container.Parent.Parent).PageCount %> " causesvalidation= "False " commandargument= "Last " commandname= "Page " text= "尾页 " />


<asp:textbox id= "txtNewPageIndex " runat= "server " width= "20px " text= ' <%# ((GridView)Container.Parent.Parent).PageIndex + 1 %> ' />
<asp:linkbutton id= "btnGo " runat= "server " causesvalidation= "False " commandargument= "-1 " commandname= "Page " text= "GO " />
</PagerTemplate>
</asp:GridView>
<br />
<asp:GridView ID= "GridView2 " runat= "server " AutoGenerateColumns= "true " AllowPaging= "true " OnPageIndexChanging= "GridView_PageIndexChanging " OnDataBound= "GridView_DataBound " >
<PagerTemplate>
每页 <%# ((GridView)Container.Parent.Parent).PageSize %> 项
共 <%# ((DataTable)((GridView)Container.Parent.Parent).DataSource).Rows.Count %> 项
<asp:linkbutton id= "btnFirst " runat= "server " Enabled= ' <%# ((GridView)Container.Parent.Parent).PageIndex != 0 %> ' causesvalidation= "False " commandargument= "First " commandname= "Page " text= "首页 " />
<asp:linkbutton id= "btnPrev " runat= "server " Enabled= " <%# ((GridView)Container.Parent.Parent).PageIndex != 0 %> " causesvalidation= "False " commandargument= "Prev " commandname= "Page " text= "上一页 " />
<asp:linkbutton id= "btnNext " runat= "server " Enabled= " <%# ((GridView)Container.Parent.Parent).PageIndex+1 != ((GridView)Container.Parent.Parent).PageCount %> " causesvalidation= "False " commandargument= "Next " commandname= "Page " text= "下一页 " />
<asp:linkbutton id= "btnLast " runat= "server " Enabled= " <%# ((GridView)Container.Parent.Parent).PageIndex+1 != ((GridView)Container.Parent.Parent).PageCount %> " causesvalidation= "False " commandargument= "Last " commandname= "Page " text= "尾页 " />
第 <asp:DropDownList ID= "drpNewPageIndex " runat= "server " AutoPostBack= "true " CausesValidation= "false " OnSelectedIndexChanged= "drpNewPageIndex_SelectedIndexChanged ">
</asp:DropDownList> 页
</PagerTemplate>
</asp:GridView>
</div>
</form>
</body>
</html>

[解决办法]
数据比较多时最好在数据库中实现分页的功能。。不然效率很低。。。

读书人网 >asp.net

热点推荐