AspNetPager分页控件使用方法

一、下载AspNetPager.dll
二、AspNetPager.dll复制于应用程序下的bin目录,打开解决方案,引用dll文件
三、 在工具栏中添加控件,这样可以支持拖拽使用
四、 要使用AspNetPager 要为其设置最基本的属性
示例:

1、前台显示界面代码Default.aspx

 View Code

 

红框为分页相关代码


2、Default.aspx.cs页面的代码

复制代码
DBAccess db = new DBAccess();
protected void Page_Load(object sender, EventArgs e)
{
if (!Page.IsPostBack)
{ BindGrid(); }
}
protected void AspNetPager1_PageChanged(object sender, EventArgs e)
{ BindGrid();
}
public void BindGrid()
{
this.AspNetPager1.RecordCount = Int32.Parse(db.GetAllCount().ToString());
int pageIndex = this.AspNetPager1.CurrentPageIndex - 1;
int pageSize = this.AspNetPager1.PageSize = 20;
Repeater1.DataSource = db.GetCurrentPage(pageIndex, pageSize);
Repeater1.DataBind();
}
三、DBAccess.cs页面的代码
using System.Data.SqlClient;
public class DBAccess
{

private SqlConnection con;
private string DBName = "tongjinet";

//创建连接对象并打开
public void Open()
{
if (con == null)
con = new SqlConnection("server=(local);uid=sa;pwd=sql;database=" + DBName);
if (con.State == ConnectionState.Closed)
con.Open();
}
//创建一个命令对象并返回该对象
public SqlCommand CreateCommand(string sqlStr)
{
Open();
SqlCommand cmd = new SqlCommand();
cmd.CommandType = CommandType.Text;
cmd.CommandText = sqlStr;
cmd.Connection = con;
return cmd;
}
//生成一个对象并返回该结果集第一行第一列
public object GetScalar(string sqlStr)
{
SqlCommand cmd = CreateCommand(sqlStr);
object obj = cmd.ExecuteScalar();
//CommadnBehavior.CloseConnection是将于DataReader的数据库链接关联起来
//当关闭DataReader对象时候也自动关闭链接
return obj;
}
//执行数据库查询并返回一个数据集 [当前页码,每页记录条数]
public DataSet GetCurrentPage(int pageIndex, int pageSize)
{
//设置导入的起始地址
int firstPage = pageIndex * pageSize;
string sqlStr = "select * from outputsell order by osid desc";
SqlCommand cmd = CreateCommand(sqlStr);
DataSet dataset = new DataSet();
SqlDataAdapter dataAdapter = new SqlDataAdapter(cmd);
dataAdapter.Fill(dataset, firstPage, pageSize, "outputsell");
cmd.Dispose();
Close();
dataAdapter.Dispose();
return dataset;
}
//获得查询数据的总条数
public object GetAllCount()
{
string sqlStr = "select count(*) from outputsell";
object obj = GetScalar(sqlStr);
return obj;
}

//关闭数据库
public void Close()
{
if (con != null)
{
con.Close();
}
}
//释放资源
public void Dispose()
{
if (con != null)
{
con.Dispose();
con = null;
}
}
}
复制代码

代码参考:http://www.cnblogs.com/taizhouxiaoba/archive/2009/03/23/1419822.html

posted @ 2021-11-01 23:46  vba是最好的语言  阅读(74)  评论(0编辑  收藏  举报