首先在网站里的 在工具栏里 反键 选择项 浏览 找到此 AsoNetPager.dll 添加此dll-->ok!
用Repeater作为显示数据
<ItemTemplate>
<!--显示自己需要的数据 DataList-->
<%# Eval("Title")%>
<br />
<hr />
</ItemTemplate>
把刚刚添加的控件 拖到页面中
选择此控件 属性--> 事件--> 双击 PageChinging 事件
前台ok!
后台
在Dbhelper类里写2方法
/// <summary>
/// 返回首行首列
/// </summary>
/// <param name="sql">查询表里一共有多少行数据</param>
/// <returns></returns>
public static int ExcuScalar(string sql ) {
SqlConnection con = new SqlConnection(ConfigurationManager.AppSettings["connectionStrings"].ToString());
SqlCommand cmd = new SqlCommand(sql,con);
con.Open();
cmd.CommandText = sql;
int i = (int)cmd.ExecuteScalar();
con.Close();
return i;
}
/// <summary>
/// 绑定数据
/// </summary>
/// <param name="sql">查询表的sql语句</param>
/// <param name="PageSize">1页多少行数据</param>
/// <param name="CurrentPageIndex">当前页数</param>
/// <returns></returns>
public static DataSet ReDataSetBind(string sql, int PageSize, int CurrentPageIndex)
{
SqlConnection con = new SqlConnection(ConfigurationManager.AppSettings["connectionStrings"].ToString());
SqlDataAdapter da = new SqlDataAdapter(sql, con);
DataSet ds = new DataSet();
da.Fill(ds, PageSize * (CurrentPageIndex - 1), PageSize, "book");
return ds;
}
在页面。cs文件里写
在页面加载方法里:
{
this.AspNetPager1.AlwaysShow = true; //显示分页控件 1页也显示
this.AspNetPager1.PageSize = 10;//设置每一页多少行数据
this.AspNetPager1.RecordCount = DBhelper.ExcuScalar("select count(*) from books");
//---------------------------------------------------开始查询数据----
this.Bind();
}
private void Bind() {
string sql = "select * from Books";
DataSet ds = DBhelper.ReDataSetBind(sql, this.AspNetPager1.PageSize, this.AspNetPager1.CurrentPageIndex);
this.Repeater1.DataSource = ds.Tables[0];
this.Repeater1.DataBind();
}
在刚才双击PageChangind的 AspNetPager1_PageChanging 事件里写
{
this.AspNetPager1.CurrentPageIndex = e.NewPageIndex;
this.Bind();
}