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