Asp.net中DataGrid控件的自定义分页

         使用实现起来虽然比较方便,但是效率不高,每次都需要读取所有页(整个记录集),而加载的只是其中一页,造成了资源的浪费,记录多又会使效率变得很低。下面通过DataGrid的自定义分页功能来减少资源使用和提高效率。<o:p></o:p>

         实现的关键是设置AllowCustomPaging属性位True,并把VirtualItemCount属性设置位总的记录数,给分页提供依据,前台的主要代码如下:<o:p></o:p>

<form id="Form1" method="post" runat="server"><o:p></o:p>

                            <TABLE id="Table1" style="FONT-SIZE: <st1:chmetcnv tcsc="0" hasspace="False" sourcevalue="9" numbertype="1" negative="False" unitname="pt" w:st="on">9pt</st1:chmetcnv>" cellSpacing="1" cellPadding="1" width="450" align="center"<o:p></o:p>

                                     border="1"><o:p></o:p>

                                     <TR><o:p></o:p>

                                               <TD><o:p></o:p>

                                                        <asp:datagrid id="DataGrid1" runat="server" Width="100%" AllowPaging="True" AllowCustomPaging="True"><o:p></o:p>

                                                                 <PagerStyle Font-Size="<st1:chmetcnv tcsc="0" hasspace="False" sourcevalue="9" numbertype="1" negative="False" unitname="pt" w:st="on">9pt</st1:chmetcnv>" Mode="NumericPages"></PagerStyle><o:p></o:p>

                                                        </asp:datagrid></TD><o:p></o:p>

                                     </TR><o:p></o:p>

                            </TABLE><o:p></o:p>

                   </form><o:p></o:p>

这里使用的数据源还是假设为NorthwindCustomers表。<o:p></o:p>

下面是访问单页的存储过程,实现方式很多,不过这个是最普通的,<o:p></o:p>

CREATE PROCEDURE [GetCustomersDataPage] <o:p></o:p>

         @PageIndex INT,<o:p></o:p>

         @PageSize  INT,<o:p></o:p>

         @RecordCount INT OUT,<o:p></o:p>

         @PageCount INT OUT<o:p></o:p>

AS<o:p></o:p>

SELECT @RecordCount = COUNT(*)  FROM   Customers<o:p></o:p>

SET @PageCount = CEILING(@RecordCount * 1.0 / @PageSize) <o:p></o:p>

DECLARE @SQLSTR NVARCHAR(1000)<o:p></o:p>

IF @PageIndex = 0 OR @PageCount <= 1<o:p></o:p>

         SET @SQLSTR =N'SELECT TOP '+STR( @PageSize )+<o:p></o:p>

'  CustomerID, CompanyName,Address,Phone  FROM   Customers ORDER BY CustomerID DESC'<o:p></o:p>

ELSE IF     @PageIndex = @PageCount - 1             <o:p></o:p>

         SET @SQLSTR =N' SELECT * FROM ( SELECT TOP '+STR( @RecordCount - @PageSize * @PageIndex )+<o:p></o:p>

'  CustomerID, CompanyName,Address,Phone  FROM   Customers ORDER BY CustomerID ASC ) TempTable  ORDER BY CustomerID DESC'<o:p></o:p>

ELSE          <o:p></o:p>

        SET @SQLSTR =N' SELECT TOP  '+STR( @PageSize )+' * FROM ( SELECT TOP '+STR( @RecordCount - @PageSize * @PageIndex )+<o:p></o:p>

'  CustomerID, CompanyName,Address,Phone  FROM   Customers ORDER BY CustomerID ASC ) TempTable ORDER BY CustomerID DESC'<o:p></o:p>

EXEC (@SQLSTR)<o:p></o:p>

<o:p> </o:p>

GO<o:p></o:p>

获取记录数和页数都采用存储过程的输出参数。<o:p></o:p>

获取数据源,这里返回一个DataSet<o:p></o:p>

先定义了连个数据成员,<o:p></o:p>

private int pageCount;//页数<o:p></o:p>

private int recordCount;//记录数<o:p></o:p>

//获取单页数据<o:p></o:p>

private static DataSet GetCustomersData(int pageIndex,int pageSize,ref int recordCount,ref int pageCount)<o:p></o:p>

{<o:p></o:p>

     string connString = ConfigurationSettings.AppSettings["ConnString"];<o:p></o:p>

     SqlConnection conn = new SqlConnection(connString);<o:p></o:p>

     SqlCommand comm = new SqlCommand("GetCustomersDataPage",conn);<o:p></o:p>

<o:p> </o:p>

     comm.Parameters.Add(new SqlParameter("@PageIndex",SqlDbType.Int));<o:p></o:p>

     comm.Parameters[0].Value = pageIndex;<o:p></o:p>

<o:p> </o:p>

     comm.Parameters.Add(new SqlParameter("@PageSize",SqlDbType.Int));<o:p></o:p>

     comm.Parameters[1].Value = pageSize;<o:p></o:p>

<o:p> </o:p>

     comm.Parameters.Add(new SqlParameter("@RecordCount",SqlDbType.Int));<o:p></o:p>

     comm.Parameters[2].Direction = ParameterDirection.Output;<o:p></o:p>

<o:p> </o:p>

     comm.Parameters.Add(new SqlParameter("@PageCount",SqlDbType.Int));<o:p></o:p>

     comm.Parameters[3].Direction = ParameterDirection.Output;<o:p></o:p>

<o:p> </o:p>

     comm.CommandType = CommandType.StoredProcedure;<o:p></o:p>

     SqlDataAdapter dataAdapter = new SqlDataAdapter(comm);<o:p></o:p>

     DataSet ds = new DataSet();<o:p></o:p>

     dataAdapter.Fill(ds);<o:p></o:p>

<o:p> </o:p>

     recordCount = (int)comm.Parameters[2].Value;<o:p></o:p>

     pageCount = (int)comm.Parameters[3].Value;<o:p></o:p>

<o:p> </o:p>

     return ds;<o:p></o:p>

}<o:p></o:p>

//绑定数据到DataGrid,同时刷新数据总记录数<o:p></o:p>

private void DataGridDataBind()<o:p></o:p>

{<o:p></o:p>

     DataSet ds = GetCustomersData(PageIndex,PageSize,ref recordCount,ref pageCount);<o:p></o:p>

     this.DataGrid1.VirtualItemCount = RecordCount;<o:p></o:p>

     this.DataGrid1.DataSource = ds;<o:p></o:p>

     this.DataGrid1.DataBind();<o:p></o:p>

}<o:p></o:p>

下面是分页的几个变量属性<o:p></o:p>

public int PageCount<o:p></o:p>

{<o:p></o:p>

     get{return this.DataGrid1.PageCount;}<o:p></o:p>

}<o:p></o:p>

<o:p> </o:p>

public int PageSize<o:p></o:p>

{<o:p></o:p>

     get{return this.DataGrid1.PageSize;}<o:p></o:p>

}<o:p></o:p>

<o:p> </o:p>

public int PageIndex<o:p></o:p>

{<o:p></o:p>

     get{return this.DataGrid1.CurrentPageIndex;}<o:p></o:p>

     set{this.DataGrid1.CurrentPageIndex = value;}<o:p></o:p>

}<o:p></o:p>

<o:p> </o:p>

public int RecordCount<o:p></o:p>

{<o:p></o:p>

     get{return recordCount;}<o:p></o:p>

}<o:p></o:p>

注册DataGrid分页事件<o:p></o:p>

//分页事件处理<o:p></o:p>

private void DataGrid1_PageIndexChanged(object source, System.Web.UI.WebControls.DataGridPageChangedEventArgs e)<o:p></o:p>

{<o:p></o:p>

     DataGrid dg = (DataGrid)source;<o:p></o:p>

     dg.CurrentPageIndex = e.NewPageIndex;<o:p></o:p>

     DataGridDataBind();<o:p></o:p>

}<o:p></o:p>

最好判断当前页面是否是第一次加载,防止重复加载两次数据,<o:p></o:p>

private void Page_Load(object sender, System.EventArgs e)<o:p></o:p>

{<o:p></o:p>

     if(!Page.IsPostBack)<o:p></o:p>

     {<o:p></o:p>

         DataGridDataBind();<o:p></o:p>

     }<o:p></o:p>

}<o:p></o:p>

显示界面如下:<o:p></o:p>

<v:shapetype o:spt="75" coordsize="21600,21600" filled="f" stroked="f" id="_x0000_t75" path="m@4@5l@4@11@9@11@9@5xe" o:preferrelative="t"><v:stroke joinstyle="miter"></v:stroke><v:formulas><v:f eqn="if lineDrawn pixelLineWidth 0"></v:f><v:f eqn="sum @0 1 0"></v:f><v:f eqn="sum 0 0 @1"></v:f><v:f eqn="prod @2 1 2"></v:f><v:f eqn="prod @3 21600 pixelWidth"></v:f><v:f eqn="prod @3 21600 pixelHeight"></v:f><v:f eqn="sum @0 0 1"></v:f><v:f eqn="prod @6 1 2"></v:f><v:f eqn="prod @7 21600 pixelWidth"></v:f><v:f eqn="sum @8 21600 0"></v:f><v:f eqn="prod @7 21600 pixelHeight"></v:f><v:f eqn="sum @10 21600 0"></v:f></v:formulas><v:path o:extrusionok="f" o:connecttype="rect" gradientshapeok="t"></v:path><o:lock v:ext="edit" aspectratio="t"></o:lock></v:shapetype><o:p></o:p>


这个例子中没有显示分页的一些参数,我们可以进一步对其进行改进。<o:p></o:p>

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值