DataGrid提供了分页功能,不过看上去功能有限,但是我们可以通过DataGrid的一些属性来获取状态以及增加首页、尾页功能按钮。这里没有使用DataGrid的自定义分页功能,如果在速度效率不是很讲究的情况下,由DataGrid自己管理分页还是不错的,付出的代价就是要把整个相关数据取出来后再删选指定页的数据。好处就是开发速度快,不需要写分页的存储过程。本文事例使用的是Sql
Server中的Northwind数据库。运行界面如下:
对于前台的显示界面,我放了一个DataGrid;四个LinkButton导向按钮;四个Literal来显示纪录状态。
剩下的就是用表格定位。
这里需要设置DataGrid的AllowPaging属性为True,同时设置AllowCustomPaging属性位false(默认为false),设置PagerStyle的Visible属性为False,使前台不显示。
前台的代码如下:
<%@Pagelanguage="c#"Codebehind="DataGridPaging.aspx.cs"AutoEventWireup="false"Inherits="ZZ.AspnetPaging.DataGridPaging"%>
<!DOCTYPEHTMLPUBLIC"-//W3C//DTDHTML4.0Transitional//EN">
<HTML>
<HEAD>
<title>DataGridPaging</title>
<metacontent="MicrosoftVisualStudio.NET7.1"name="GENERATOR">
<metacontent="C#"name="CODE_LANGUAGE">
<metacontent="JavaScript"name="vs_defaultClientScript">
<metacontent="http://schemas.microsoft.com/intellisense/ie5"name="vs_targetSchema">
</HEAD>
<body>
<formid="Form1"method="post"runat="server">
<TABLEid="Table1"style="FONT-SIZE:9pt"cellSpacing="1"cellPadding="1"width="450"align="center"
border="1">
<TR>
<TD><asp:datagridid="DataGrid1"runat="server"PageSize="5"Width="100%"AllowPaging="True">
<HeaderStyleFont-Size="9pt"></HeaderStyle>
<FooterStyleFont-Size="9pt"></FooterStyle>
<PagerStyleVisible="False"Font-Size="9pt"Mode="NumericPages"></PagerStyle>
</asp:datagrid></TD>
</TR>
</TABLE>
<TABLEid="Table2"style="FONT-SIZE:9pt"cellSpacing="1"cellPadding="1"width="450"align="center"
border="1">
<TR>
<TDstyle="WIDTH:207px">
<asp:linkbuttonid="LBtnFirst"runat="server"CommandName="First">首页</asp:linkbutton>
<asp:linkbuttonid="LBtnPrev"runat="server"CommandName="Prev">上一页</asp:linkbutton>
<asp:linkbuttonid="LBtnNext"runat="server"CommandName="Next">下一页</asp:linkbutton>
<asp:linkbuttonid="LBtnLast"runat="server"CommandName="Last">尾页</asp:linkbutton></TD>
<TD>第
<asp:literalid="LtlPageIndex"runat="server"></asp:literal>页共
<asp:literalid="LtlPageCount"runat="server"></asp:literal>页每页
<asp:literalid="LtlPageSize"runat="server"></asp:literal>条共
<asp:literalid="LtlRecordCount"runat="server"></asp:literal>条
</TD>
</TR>
</TABLE>
</form>
</body>
</HTML>
后台cs文件代码,DataGridPaging类从System.Web.UI.Page继承,在数据绑定时需要注意没有数据的情况(0页时),以及当页数减少时避免前台正在反页导致缺页。
usingSystem;
usingSystem.Collections;
usingSystem.ComponentModel;
usingSystem.Data;
usingSystem.Drawing;
usingSystem.Web;
usingSystem.Web.SessionState;
usingSystem.Web.UI;
usingSystem.Web.UI.WebControls;
usingSystem.Web.UI.HtmlControls;
usingSystem.Data.SqlClient;
usingSystem.Configuration;
namespaceZZ.AspnetPaging
{
publicclassDataGridPaging:System.Web.UI.Page
{
privatestaticstringconnString=ConfigurationSettings.AppSettings["ConnString"];
privateintrecordCount;
privateintpageCount;
protectedSystem.Web.UI.WebControls.LinkButtonLBtnFirst;
protectedSystem.Web.UI.WebControls.LinkButtonLBtnPrev;
protectedSystem.Web.UI.WebControls.LinkButtonLBtnNext;
protectedSystem.Web.UI.WebControls.LinkButtonLBtnLast;
protectedSystem.Web.UI.WebControls.LiteralLtlPageIndex;
protectedSystem.Web.UI.WebControls.LiteralLtlPageCount;
protectedSystem.Web.UI.WebControls.LiteralLtlPageSize;
protectedSystem.Web.UI.WebControls.LiteralLtlRecordCount;
protectedSystem.Web.UI.WebControls.DataGridDataGrid1;
privatevoidPage_Load(objectsender,System.EventArgse)
{
if(!Page.IsPostBack)
{
DataGridDataBind();
}
}
//绑定数据
privatevoidDataGridDataBind()
{
DataSetds=GetCustomersData();
recordCount=ds.Tables[0].Rows.Count;
//获取当前的页数
pageCount=(int)Math.Ceiling(recordCount*1.0/PageSize);
//避免纪录从有到无时,并且已经进行过反页的情况下CurrentPageIndex>PageCount出错
if(recordCount==0)
{
this.DataGrid1.CurrentPageIndex=0;
}
elseif(this.DataGrid1.CurrentPageIndex>=pageCount)
{
this.DataGrid1.CurrentPageIndex=pageCount-1;
}
this.DataGrid1.DataSource=ds;
this.DataGrid1.DataBind();
NavigationStateChange();
}
#regionWeb窗体设计器生成的代码
overrideprotectedvoidOnInit(EventArgse)
{
//
//CODEGEN:该调用是ASP.NETWeb窗体设计器所必需的。
//
InitializeComponent();
base.OnInit(e);
}
///<summary>
///设计器支持所需的方法-不要使用代码编辑器修改
///此方法的内容。
///</summary>
privatevoidInitializeComponent()
{
this.LBtnFirst.Click+=newSystem.EventHandler(this.LBtnNavigation_Click);
this.LBtnPrev.Click+=newSystem.EventHandler(this.LBtnNavigation_Click);
this.LBtnNext.Click+=newSystem.EventHandler(this.LBtnNavigation_Click);
this.LBtnLast.Click+=newSystem.EventHandler(this.LBtnNavigation_Click);
this.Load+=newSystem.EventHandler(this.Page_Load);
}
#endregion
privatevoidLBtnNavigation_Click(objectsender,System.EventArgse)
{
LinkButtonbtn=(LinkButton)sender;
switch(btn.CommandName)
{
case"First":
PageIndex=0;
break;
case"Prev"://if(PageIndex>0)
PageIndex=PageIndex-1;
break;
case"Next"://if(PageIndex<PageCount-1)
PageIndex=PageIndex+1;
break;
case"Last":
PageIndex=PageCount-1;
break;
}
DataGridDataBind();
}
//数据绑定
publicstaticDataSetGetCustomersData()
{
SqlConnectionconn=newSqlConnection(connString);
stringsqlStr="SELECTCustomerID,CompanyName,Address,PhoneFROMCustomers";
SqlCommandcomm=newSqlCommand(sqlStr,conn);
SqlDataAdapterdataAdapter=newSqlDataAdapter(comm);
DataSetds=newDataSet();
dataAdapter.Fill(ds);
returnds;
}
///<summary>
///控制导航按钮或数字的状态
///</summary>
publicvoidNavigationStateChange()
{
if(PageCount<=1)//(RecordCount<=PageSize)//小于等于一页
{
this.LBtnFirst.Enabled=false;
this.LBtnPrev.Enabled=false;
this.LBtnNext.Enabled=false;
this.LBtnLast.Enabled=false;
}
else//有多页
{
if(PageIndex==0)//当前为第一页
{
this.LBtnFirst.Enabled=false;
this.LBtnPrev.Enabled=false;
this.LBtnNext.Enabled=true;
this.LBtnLast.Enabled=true;
}
elseif(PageIndex==PageCount-1)//当前为最后页
{
this.LBtnFirst.Enabled=true;
this.LBtnPrev.Enabled=true;
this.LBtnNext.Enabled=false;
this.LBtnLast.Enabled=false;
}
else//中间页
{
this.LBtnFirst.Enabled=true;
this.LBtnPrev.Enabled=true;
this.LBtnNext.Enabled=true;
this.LBtnLast.Enabled=true;
}
}
if(RecordCount==0)//当没有纪录时DataGrid.PageCount会显示1页
this.LtlPageCount.Text="0";
else
this.LtlPageCount.Text=PageCount.ToString();
if(RecordCount==0)
this.LtlPageIndex.Text="0";
else
this.LtlPageIndex.Text=(PageIndex+1).ToString();//在有页数的情况下前台显示页数加1
this.LtlPageSize.Text=PageSize.ToString();
this.LtlRecordCount.Text=RecordCount.ToString();
}
//总页数
publicintPageCount
{
get{returnthis.DataGrid1.PageCount;}
}
//页大小
publicintPageSize
{
get{returnthis.DataGrid1.PageSize;}
}
//页索引,从零开始
publicintPageIndex
{
get{returnthis.DataGrid1.CurrentPageIndex;}
set{this.DataGrid1.CurrentPageIndex=value;}
}
//纪录总数
publicintRecordCount
{
get{returnrecordCount;}
set{recordCount=value;}
}
}
}
如果需要追求执行效率,而且数据量比较大的情况下建议使用DataGrid的自定义分页功能。存储过程执行只取一页的情况。如果有什么好的建议或发现问题请在Blog上留言。
优质内容筛选与推荐>>
1、记一次基于Cloudflare服务的爬虫2、类装载器子系统3、iOS 6.0之后支持一个页面横屏的方法4、java知识点整理 - 25、CDMA技术九问九答
长按二维码向我转账
受苹果公司新规定影响,微信 iOS 版的赞赏功能被关闭,可通过二维码转账支持公众号。