AspNetPager比较重要的几个属性
CurrentPageIndex 获取或设置当前显示页的索引。
PageSize 获取或设置每页显示的项数。
PageCount 获取所有要分页的记录需要的总页数。
CustomInfoHTML 获取或设置在显示在用户自定义信息区的用户自定义HTML文本内容。
FirstPageText 获取或设置为第一页按钮显示的文本。
LastPageText 获取或设置为最后一页按钮显示的文本。
PrevPageText 获取或设置为上一页按钮显示的文本。
RecordCount 获取或设置需要分页的所有记录的总数。
AlwaysShow 获取或设置一个值,该值指定是否总是显示AspNetPager分页按件,即使要分页的数据只有一页。
ShowPageIndex 获取或设置一个值,该值指示是否在页导航元素中显示页索引数值按钮。
ShowPrevNext 获取或设置一个值,该值指示是否在页导航元素中显示上一页和下一页按钮。
UrlPaging 获取或设置是否启用url来传递分页信息。
1.aspnetpager使用传统方式分页
使用Repeater控件和AspNetPager控件实现传统分页,
private void BindRepeater()
{
string sql = "select * from tb_Roles";//自定义的SQL语句
int recordcount;
SqlCommand cmd = new SqlCommand(sql, GetConnection());
cmd.CommandType = CommandType.Text;
SqlDataAdapter ada = new SqlDataAdapter(cmd);
DataSet ds = new DataSet();
int startRow = (this.AspNetPager1.CurrentPageIndex - 1) * this.AspNetPager1.PageSize;
ada.Fill(ds, startRow, this.AspNetPager1.PageSize, "table");
recordcount = GetPageRecord(sql);
this.AspNetPager1.RecordCount = recordcount;
this.Repeater1.DataSource = ds;
this.Repeater1.DataBind();
}
另外在AspNetPager分页控件PageChanged事件中绑定。
protected void AspNetPager1_PageChanged(object sender, EventArgs e)
{
this.BindRepeater();
}
另外获取总记录数的方法
public int GetPageRecord(string sql)
{
sql = System.Text.RegularExpressions.Regex.Replace(sql, "ORDER BY.*", "");
sql = "select count(*) from (" + sql + ") as temp";
SqlCommand cmd = new SqlCommand(sql, GetConnection());
cmd.Connection.Open();
int recordcount = (int)cmd.ExecuteScalar();
cmd.Connection.Close();
return recordcount;
}
来自 “ ITPUB博客 ” ,链接:https://blog.itpub.net/12639172/viewspace-660571/ ,如需转载,请注明出处,否则将追究法律责任。