asp.net分页控件AspNetPager的使用,使用传统分页和存储过程分页

记录一下,分页控件的使用,先记录AspNetPager分页控件的使用

AspNetPager分页的控件的下载地址是:http://www.webdiyer.com/Controls/AspNetPager/Downloads

在线帮助文档:http://www.webdiyer.com/AspNetPagerDocs/index.html

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;
}

2.aspnetpager中使用存储过程分页

调用存储过程获取数据

private void BindListView()
{
    this.AspNetPager2.RecordCount = GetPageRecord("select * from tb_Groups");//获取总记录数
    SqlCommand cmd = new SqlCommand("pager", GetConnection());
    cmd.CommandType = CommandType.StoredProcedure;//使用存储过程
    int startRow = (this.AspNetPager2.CurrentPageIndex - 1) * this.AspNetPager2.PageSize;//起始点
    int endRow = this.AspNetPager2.CurrentPageIndex * this.AspNetPager2.PageSize;
    SqlParameter[] parameters = new SqlParameter[]{
        new SqlParameter("@startIndex",startRow),
        new SqlParameter("@endIndex",endRow),
        new SqlParameter("@docount",'0'),
    };
    cmd.Parameters.AddRange(parameters);
    SqlDataAdapter ada = new SqlDataAdapter(cmd);
    DataSet ds = new DataSet();
    ada.Fill(ds,"table");
    this.GridView1.DataSource = ds;
    this.GridView1.DataBind();
}

同样需要在PageChanged事件调用绑定方法

最后存储过程的代码是用AspNetPager控件生成的代码,根据向导填入相关信息,就可以生成存储过程的代码



  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值