AspNetPager使用方法

来源:互联网 发布:药店gsp认证软件 编辑:程序博客网 时间:2024/04/30 19:29

不同于DataGrid控件,AspNetPager分页控件本身并不显示任何数据,而只显示页导航元素,数据在页面上的显示方式与该控件无关。该控件可以为DataGrid、DataList、Repeater以及自定义控件进行分页,配合Sql存储过程,分页性能较使用DataGrid分页有明显提升,尤其是当数据量大时性能可提升数倍!

    AspNetPager 2.0 中新增了通过Url来分页的功能,这使得访问者可以直接输入相应的Url来访问任何页面,并且搜索引擎也可以直接检索每个页面,若使用DataGrid的分页功能,这是无法实现的。

要使用 AspNetPager 分页控件,必须最少指定它的 RecordCount 属性,指定并编写 PageChanged 事件的处理程序。 RecordCount 属性指定要分页的所有数据的总项数,若未指定该值或该值小于等于 PageSize ,则AspNetPager控件不会显示任何内容。 若未指定并编写 PageChanged 事件处理程序,则当用户点击页导航元素或在页索引文本框中手式输入页索引并提交时AspNetPager不会跳转到指定的页。 AspNetPager控件的分页方法和DataGrid基本相同,即在它的 PageChanged 事件处理程序中将传递事件数据的 PageChangedEventArgs 的 NewPageIndex值赋给 AspNetPager的 CurrentPageIndex属性,然后重新将新的数据与数据显示控件绑定。

RecordCount :当页面第一次加载时,应以编程方式将从存储过程或Sql语句中返回的数据表中所有要分页的记录的总数赋予该属性,AspNetPager会将其保存的ViewState中并在页面回发时从ViewState中获取该值,因此避免了每次分页都要访问数据库而影响分页性能。AspNetPager根据要分页的所有数据的总项数和 PageSize 属性来计算显示所有数据需要的总页数,即 PageCount的值。

PageSize :该值获取或设置数据呈现控件每次要显示数据表中的的数据的项数,AspNetPager根据该值和 RecordCount 来计算显示所有数据需要的总页数,即 PageCount的值

前台页面:

<%@ Page Language="C#" AutoEventWireup="true" CodeFile="Paging.aspx.cs" Inherits="Paging" %>

<%@ Register Assembly="AspNetPager" Namespace="Wuqi.Webdiyer" TagPrefix="webdiyer" %>

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

<html xmlns="http://www.w3.org/1999/xhtml" >
<head runat="server">
    <title>无标题页</title>
</head>
<body style="text-align: center">
    <form id="form1" runat="server">
    <div style="text-align: center">
        <asp:GridView ID="GridView1" runat="server" PageSize="3" Width="379px" AutoGenerateColumns="False">
            <Columns>
                <asp:BoundField DataField="proID" />
                <asp:BoundField DataField="proName" />
                <asp:BoundField DataField="proNum" />
                <asp:BoundField DataField="proPrice" />
                <asp:BoundField DataField="totalPrice" />
            </Columns>
        </asp:GridView>
   
    </div>
        &nbsp;
        &nbsp;&nbsp;<br />
        <br />
        <webdiyer:AspNetPager ID="pageBind" runat="server" AlwaysShow="True" FirstPageText="<<"
            HorizontalAlign="Right" LastPageText=">>" NextPageText=">" NumericButtonCount="5"
            NumericButtonTextFormatString="-{0}-" OnPageChanged="pageBind_PageChanged" PageSize="5"
            PrevPageText="<" ShowBoxThreshold="2" ShowCustomInfoSection="Left" ShowInputBox="Always"
            Width="382px">
        </webdiyer:AspNetPager>
    </form>
</body>
</html>

后台页面:

using System;
using System.Data;
using System.Configuration;
using System.Collections;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;

using System.Data.OleDb;
using System.Text;
using Wuqi.Webdiyer;


public partial class Paging : System.Web.UI.Page
{
    String MyConn;
    OleDbConnection Conn;
    protected void Page_Load(object sender, EventArgs e)
    {

        MyConn = ConfigurationManager.AppSettings["Conn"] + HttpContext.Current.Server.MapPath(ConfigurationManager.AppSettings["Data"]) + ";";
        Conn = new OleDbConnection(MyConn);     //初始化连接
        if (!this.IsPostBack)
        {
            string sql;
            sql = "select count(*) from shop";
            OleDbCommand Cmd = new OleDbCommand(sql,Conn);
            Conn.Open();
            this.pageBind.RecordCount = (int)Cmd.ExecuteScalar();
            Conn.Close();
            BindData();
        }
    }
    protected void BindData()
    {
        string sql;
        sql = "select * from shop";
      
        OleDbDataAdapter Ad = new OleDbDataAdapter(sql,Conn);
        DataSet Ds = new DataSet();
        Ad.Fill(Ds, this.pageBind.PageSize * (this.pageBind.CurrentPageIndex - 1), this.pageBind.PageSize, "zjt");

     
        this.GridView1.DataSource = Ds.Tables["zjt"];
        this.GridView1.DataBind();
        AddCustomText();
    }
    public void AddCustomText()
    { 
        pageBind.CustomInfoText = "记录总数:<font color=/"blue/"><b>" + pageBind.RecordCount.ToString() + "</b></font>";
        pageBind.CustomInfoText += " 总页数:<font color=/"blue/"><b>" + pageBind.PageCount.ToString() + "</b></font>";
        pageBind.CustomInfoText += " 当前页:<font color=/"red/"><b>" + pageBind.CurrentPageIndex.ToString() + "</b></font>";

    }
       protected void pageBind_PageChanged(object src, PageChangedEventArgs e)
      {
        this.pageBind.CurrentPageIndex = e.NewPageIndex;
        BindData();
    }
}

原创粉丝点击