AspNetPager分页控件使用

GridView、DataList和Repeater分页功能是我们在项目开发中经常遇到的。GridView也自带分页功能,但让人总觉得不是特别完善,如果你也懒得自己去写一些复杂的分页功能,那么你可以使用别人写好的分页控件。下面以AspNetPager为例:
一、添加AspNetPager.dll引用:
     1.新建一个“网站”。
    2.在“解决方案资源管理器”选中网站根目右键“添加引用”,在“浏览”选项中找到添加引用的“AspNetPager.dll”文件。
3.在“工具箱”右键单击“选择项”添加“AspNetPager.dll”文件,完成后,分页控件的图标就出现在工具栏。
二、页面设计:
   
    1.打开“Default.aspx”页面,在工具栏里拖一个GridView放在页面上,然后把分页控件拖到GridView下.切换到“源视图”在<webdiyer:aspnetpager ......></webdiyer:aspnetpager>可以设置一些属性来控制分页控件样式(具体属性可参考: [url]http://www.webdiyer.com/AspNetPager/docs/index.html[/url])。
    2.web.config:
      <appSettings>
   <add key="Conn" value="server=(local);database=test1;user id=sa;pwd=19830714"/>
      </appSettings>
    3.数据访问层:
using System;
using System.Data;
using System.Configuration;
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.SqlClient;
/// <summary>
/// DataBase 的摘要说明
/// </summary>
namespace DataAccessLayer
{
    public class DataBase:IDisposable
    {
        protected SqlConnection Connection ;
        protected string ConnectionString;
        /// <summary>
        /// 默认构造函数
        /// </summary>
        public DataBase()
        {
            ConnectionString = ConfigurationManager.AppSettings["Conn"];
        }
        /// <summary>
        /// Dispose方法,释放建立连接时占用的资源
        /// </summary>
        public void Dispose()
        {
           if(Connection!=null)
           {
               Connection.Dispose();
               Connection = null;
           }
        }
        /// <summary>
        /// 析构函数,关闭连接释放资源
        /// </summary>
        ~DataBase()
        {
            try
            {
               if(Connection!=null)
               {
                   Connection.Close();
               }
            }
            catch
            { }
            try
            {
                Dispose();
            }
            catch
            { }
        }
        /// <summary>
        /// Open方法打开连接
        /// </summary>
        public void Open()
        {
           if(Connection==null)
           {
               Connection = new SqlConnection(ConnectionString);
           }
            if(Connection.State.ToString()=="Closed")
            {
                Connection.Open();
            }
        }
        /// <summary>
        /// Close方法关闭连接
        /// </summary>
        public void Close()
        {
           if(Connection!=null)
           {
               Connection.Close();
           }
        }
        /// <summary>
        /// GetDataSet方法
        /// </summary>
        /// <param name="SqlStrings">SQL语句</param>
        /// <param name="Pager1">当前记录数</param>
        /// <param name="Pager2">每页显示记录数</param>
        /// <param name="TableName">表名</param>
        /// <returns>DataSet</returns>
        public DataSet GetDataSet(string SqlStrings, int Pager1, int Pager2, string TableName)
        {
                Open();
                SqlDataAdapter sda = new SqlDataAdapter(SqlStrings, Connection);
                DataSet ds = new DataSet();
                sda.Fill(ds, Pager1, Pager2, TableName);
                return ds;
                Close();
        }
        /// <summary>
        /// ExecuteScalar方法,接受一个SQL语句,返回首行首列值
        /// </summary>
        /// <param name="SqlStrings">SQL语句</param>
        /// <returns>返回首行首列值</returns>
        public string ExecuteScalar(string SqlStrings)
        {
            Open();
            string count="";
            try
            {
                SqlCommand cmd = new SqlCommand(SqlStrings,Connection );
                count = cmd.ExecuteScalar().ToString();
            }
            catch
            { }
            finally
            {
                Close();
            }
            return count;
        }
    }
}
     4.业务逻辑层:
using System;
using System.Data;
using System.Configuration;
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 DataAccessLayer;
/// <summary>
/// Pager 的摘要说明
/// </summary>
namespace BussinessLogicLayer
{
    public class Pager
    {
        public Pager()
        {
            //
            // TODO: 在此处添加构造函数逻辑
            //
        }
        /// <summary>
        /// GridViewDataSet方法,返回一个DataSet
        /// </summary>
        /// <param name="Pager1">当前记录数</param>
        /// <param name="Pager2">每页显示记录数</param>
        /// <param name="TableName">表名</param>
        /// <returns>DataSet</returns>
        public DataSet GridViewDataSet(int Pager1, int Pager2, string TableName)
        {
            DataBase db = new DataBase();
            string sql = "select * from Customer order by Customer_CreateTime desc";
            DataSet ds = db.GetDataSet(sql, Pager1, Pager2, TableName);
            return ds;
        }
        /// <summary>
        /// GetCount方法,返回表中记录数
        /// </summary>
        /// <returns>返回表中记录数</returns>
        public int GetCount()
        {
            int count = 0;
            DataBase db = new DataBase();
            string sql = "select count(*) from Customer";
            count = Convert .ToInt32(db.ExecuteScalar(sql));
            return count;
        }
    }
}
     5.表示层:Default.aspx.cs
using System;
using System.Data;
using System.Configuration;
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.Collections.Generic;
using System.Data.SqlClient;
using BussinessLogicLayer;
public partial class _Default : System.Web.UI.Page
{
    protected void Page_Load(object sender, EventArgs e)
    {
        Pager pager=new Pager ();
        this.AspNetPager1.RecordCount = pager.GetCount();//总记录数
        if(!Page.IsPostBack)
        {
            GridViewBind();
        }
    }
    public void GridViewBind()
    {
        Pager pager = new Pager();
        int Pager1 = AspNetPager1.PageSize * (AspNetPager1.CurrentPageIndex-1);
        int Pager2 = AspNetPager1.PageSize;
        DataSet ds = pager.GridViewDataSet(Pager1, Pager2, "Customer");
        GridView1.DataSource = ds.Tables["Customer"];
        GridView1.DataBind();
        //显示记录信息
        AspNetPager1.CustomInfoHTML = "记录总数:" + AspNetPager1.RecordCount.ToString() + "";
        AspNetPager1.CustomInfoHTML += " 总页数:" + AspNetPager1.PageCount.ToString() + "";
        AspNetPager1.CustomInfoHTML += " 当前页:" + AspNetPager1.CurrentPageIndex.ToString() + "";
    }
    protected void AspNetPager1_PageChanged(object src, EventArgs e)
    {//PageChanged事件
        GridViewBind();
    }

}

你可能感兴趣的:(职场,休闲,AspNetPager,分页控件)