GridView的本身支持分页,一般情况下我们可以通过如下几个步骤实现分页:
1、更改GrdView控件的AllowPaging属性为true;
2、更改GrdView控件的PageSize属性为一个数值(默认为10);
3、更改GrdView控件的PageSetting属性;
4、为GridView添加事件:OnPageIndexChanging
大多数情况下通过上边的步骤就可以实现GridView的分页了,当然很多代码需要你自己来写。
但是有时候我们需要自定义分页模板,还希望能通过更统一以及更少的代码来实现这个分页功能。考虑到这些问题,我实现了一个通用的GridView分页模型,发布出来,仅供大家参考。
1、创建一个基础页面类
BasePage,继承System.Web.UI.Page
在这个基类中,创建几个通用的分页属性和方法,然后需要分页的页面都继承这个基类。
public class BasePage : System.Web.UI.Page |
protected int GridView_RecordCount = 0; |
/// 需要设置GridView_RecordCount |
/// 如果一次性绑定全部数据,不需要使用自定义,GridView可以通过DataSource自己获取 |
protected bool IsUseCustomRecordCount = false ; |
/// 在具体的页面类中重写这个方法,在PageTurn方法中就会调用重写的方法,以实现分页后的数据重新绑定 |
protected virtual void GridView_DataBind() |
/// <param name="sender">跳转按钮</param> |
/// <param name="e"></param> |
protected void GridView_PageTurn( object sender, EventArgs e) |
System.Web.UI.WebControls.Button btnGoPage; |
System.Web.UI.WebControls.GridView GridView1; |
System.Web.UI.WebControls.TextBox txtGoPage; |
btnGoPage = (System.Web.UI.WebControls.Button)sender; |
GridView1 = (System.Web.UI.WebControls.GridView)btnGoPage.NamingContainer.Parent.Parent; |
txtGoPage = (System.Web.UI.WebControls.TextBox)GridView1.BottomPagerRow.FindControl( "txtGoPage" ); |
MessageBox.Show( this .Page, "页码输入框和跳转按钮都必须在GridView的分页模板中!" ); |
if (! string .IsNullOrEmpty(txtGoPage.Text.Trim())) |
if ( int .TryParse(txtGoPage.Text.Trim(), out pageIndex)) |
if (pageIndex >= 0 && pageIndex < GridView1.PageCount) |
GridView1.PageIndex = pageIndex; |
MessageBox.Show( this .Page, "无效的页码!" ); |
/// <param name="sender"></param> |
/// <param name="e"></param> |
protected void GridView_PageIndexChanging( object sender, System.Web.UI.WebControls.GridViewPageEventArgs e) |
System.Web.UI.WebControls.GridView GridView1 = sender as System.Web.UI.WebControls.GridView; |
GridView1.PageIndex = e.NewPageIndex; |
/// GridView数据绑定完毕之后触发,显示记录数 |
/// <param name="sender"></param> |
/// <param name="e"></param> |
protected void GridView_DataBound( object sender, EventArgs e) |
System.Web.UI.WebControls.GridView GridView1 = sender as System.Web.UI.WebControls.GridView; |
if (GridView1.BottomPagerRow == null ) |
GridView1.BottomPagerRow.Visible = true ; |
System.Web.UI.WebControls.Label lblRecorCount = (System.Web.UI.WebControls.Label)GridView1.BottomPagerRow.FindControl( "lblRecorCount" ); |
if (IsUseCustomRecordCount) |
lblRecorCount.Text = GridView_RecordCount.ToString(); |
if (GridView1.DataSource == null ) |
if (GridView1.DataSource.GetType() == typeof (DataView)) |
lblRecorCount.Text = ((DataView)GridView1.DataSource).Count.ToString(); |
else if (GridView1.DataSource.GetType() == typeof (DataTable)) |
lblRecorCount.Text = ((DataTable)GridView1.DataSource).Rows.Count.ToString(); |
else if (GridView1.DataSource.GetType() == typeof (DataSet)) |
lblRecorCount.Text = ((DataSet)GridView1.DataSource).Tables[0].Rows.Count.ToString(); |
else if (GridView1.DataSource is Array) |
lblRecorCount.Text = ((Array)GridView1.DataSource).Length.ToString(); |
else if (GridView1.DataSource.GetType() is System.Collections.IList) |
lblRecorCount.Text = ((System.Collections.IList)GridView1.DataSource).Count.ToString(); |
else if (GridView1.DataSource.GetType() is System.Collections.ICollection) |
lblRecorCount.Text = ((System.Collections.ICollection)GridView1.DataSource).Count.ToString(); |
TspOALog.Write( "绑定到GridView的数据类型未知:" + GridView1.DataSource.GetType()); |
2、页面cs文件继承基类:BasePage
public partial class NewsList: BasePage
3、在页面添加一个GridView并设置相关属性
< asp:GridView ID = "GridView1" BorderStyle = "Solid" BorderWidth = "1px" BorderColor = "#73B7EA" |
Width = "100%" runat = "server" AutoGenerateColumns = "False" HorizontalAlign = "Center" |
BackColor = "White" CellPadding = "4" GridLines = "None" OnPageIndexChanging = "GridView_PageIndexChanging" PageSize = "10" |
AllowPaging = "true" OnDataBound = "GridView_DataBound" > |
< div style = "text-align: center;" >< span style = "color: Blue" > |
共有< asp:label id = "lblRecorCount" runat = "server" ></ asp:label >条记录 |
第< asp:Label ID = "lblcurPage" ForeColor = "Blue" runat = "server" Text='<%# ((GridView)Container.Parent.Parent).PageIndex+1 %>'></ asp:Label >页/共< asp:Label ID = "lblPageCount" ForeColor = "blue" runat = "server" Text='<%# ((GridView)Container.Parent.Parent).PageCount %>'></ asp:Label >页</ span > |
< asp:LinkButton ID = "cmdFirstPage" runat = "server" CommandName = "Page" CommandArgument = "First" Enabled="<%# ((GridView)Container.Parent.Parent).PageIndex!=0 %>">首页</ asp:LinkButton > |
< asp:LinkButton ID = "cmdPreview" runat = "server" CommandArgument = "Prev" CommandName = "Page" Enabled="<%# ((GridView)Container.Parent.Parent).PageIndex!=0 %>">上一页</ asp:LinkButton > |
< asp:LinkButton ID = "cmdNext" runat = "server" CommandName = "Page" CommandArgument = "Next" Enabled="<%# ((GridView)Container.Parent.Parent).PageIndex!=((GridView)Container.Parent.Parent).PageCount-1 %>">下一页</ asp:LinkButton > |
< asp:LinkButton ID = "cmdLastPage" runat = "server" CommandArgument = "Last" CommandName = "Page" Enabled="<%# ((GridView)Container.Parent.Parent).PageIndex!=((GridView)Container.Parent.Parent).PageCount-1 %>">尾页</ asp:LinkButton > |
到< asp:TextBox ID = "txtGoPage" runat = "server" Text='<%# ((GridView)Container.Parent.Parent).PageIndex+1 %>' Width="30px" CssClass="simpletextbox"></ asp:TextBox >页 |
< asp:Button ID = "Button3" runat = "server" Width = "40px" OnClick = "GridView_PageTurn" Text = "跳转" CssClass = "simplebutton" /></ div > |
主要是:
AllowPaging:允许GridView分页
PageSize:默认10
OnDataBound:绑定到基类的事件,用于显示总记录数
OnPageIndexChanging:绑定到基类的事件,用于分页的数据处理
PagerTemplate:包括记录数,当前页,总页数,首页,尾页,上一页,下一页,页码跳转
其中页码跳转事件绑定到基类中的:GridView_PageTurn
4、最后编写获取数据的方法
重写基类的GridView_DataBind方法:其中查询数据,然后绑定到GridView。
protected override void GridView_DataBind() |
//获取数据,可以是获取全部,也可以是只获取当前页数据 |
//如果你想自己设置总记录数,可以使用IsUseCustomRecordCount |
IsUseCustomRecordCount= true ; |
GridView_RecordCount=10000; |
GridView1.DataSource = dv; |
以后只要有需要分页的列表,继承BasePage,然后设置相关属性,编写数据获取代码就可以了。不必在每个页面都编写OnPageIndexChanging,OnDataBound,以及页码跳转的代码,统一实现,节省工作量。
你还可以使用统一的PageSize,只需要在BasePage添加一个属性,然后在GridView中设置PageSize的属性。
|