aspnetpager是个比较好用的分页控件。
可以通过简单的代码进行相应的绑定,代码如下:
private void QueryData() { try { DateTime dtBeg, dtEnd; if (!DateTime.TryParse(this.txtBeg.Value.Trim(), out dtBeg)) { ClientScript.RegisterClientScriptBlock(this.GetType(), "UserBookList", "<mce:script type="text/javascript"><!-- alert('开始时间输入格式不正确,请确认!'); // --></mce:script>"); return; } if (!DateTime.TryParse(this.txtEnd.Value.Trim(), out dtEnd)) { ClientScript.RegisterClientScriptBlock(this.GetType(), "UserBookList", "<mce:script type="text/javascript"><!-- alert('结束时间输入格式不正确,请确认!'); // --></mce:script>"); return; } UserRight userCtrl = new UserRight(); int recordCnt = 0; DataTable dt = userCtrl.SearchRightList(this.txtPt.Value.Trim(), int.Parse(this.ddlQueryType.SelectedValue), dtBeg, dtEnd, this.AspNetPager1.CurrentPageIndex, this.AspNetPager1.PageSize, out recordCnt); if (null != dt) { this.GridView1.DataSource = dt; this.GridView1.DataBind(); //分页 this.AspNetPager1.RecordCount = recordCnt; this.AspNetPager1.ShowCustomInfoSection = Wuqi.Webdiyer.ShowCustomInfoSection.Left; this.AspNetPager1.CustomInfoHTML = "第" + this.AspNetPager1.CurrentPageIndex + "/" + this.AspNetPager1.PageCount + "页" + ",共<font color='#ff0000'><b>" + recordCnt + "</b></font>条"; } } catch (Exception ex) { ClientScript.RegisterClientScriptBlock(this.GetType(), "test", "<mce:script type="text/javascript"><!-- alert('查询出错,请重试!'); // --></mce:script>"); } }
在查询按钮的点击事件如下调用QeuryData即可:
protected void btnQuery_Click(object sender, EventArgs e) { AspNetPager1.CurrentPageIndex = 1; QueryData(); }
aspnetpager的pagechanaged事件如下:
protected void AspNetPager1_PageChanged(object sender, EventArgs e) { QueryData(); }
当翻页的时候,可以以url方式提交。此时需要指定UrlRewritePattern ,如果需要该属性有效,需要 UrlPaging 的值为true
用法如下:
AspNetPager1.UrlRewritePattern = "/UI/list.aspx?msg=%msg%&page={0}";
this.AspNetPager1.RecordCount =TotalCount;
用“{0}”占位符来表示AspNetPager分页控件的当页页索引值,用“%参数名%”表示Url中相应的参数的值
官网说明文档如下:
获取或设置要URL的重写格式。
备注
该属性仅当 UrlPaging 的值为true时才有效。要重写的Url路径可以是相对于当前的Url路径,如:../pagelist_{0}.aspx ,也可以是绝对路径,如:http://www.webdiyer.com/articles/{0}.aspx ,用“{0}”占位符来表示AspNetPager分页控件的当页页索引值,用“%参数名%”表示Url中相应的参数的值。如果不设置该值,默认值为当前 页面路径加下划线后跟页索引, 如当前Url是 http://www.webdiyer.com/articlelist.aspx ,不设置 UrlRewritePattern 时,第一页的默认路径就是 http://www.webdiyer.com/articlelist_1.aspx ,第二页就是 http://www.webdiyer.com/articlelist_2.asp ,依次类推。以下举例说明如何设置重写格式:
1、原始Url:http://www.webdiyer.com/articlelist.aspx?type=aspnet&page=2(其中参数page=2为AspNetPager传递的页索引参数,这个参数名可以通过UrlPageIndexName 属性设置,默认值为“page”)
重写为:http://www.webdiyer.com/articlelist/aspnet/2.aspx ,则应设置:UrlRewritePattern="./articlelist/%type%/{0}.aspx";
2、原始Url:http://www.webdiyer.com/articlelist.aspx?type=dotnet& subtype=aspnet&page=3 (其中参数page=3为AspNetPager传递的页索引参数,这个参数名可以通过UrlPageIndexName 属性设置,默认值为“page”)
重写为:http://www.webdiyer.com/articlelist/dotnet/aspnet/page_3.aspx ,则应设置:UrlRewritePattern="./articlelist/%type%/%subtype%/page_{0}.aspx";
参阅:
http://www.webdiyer.com/Controls/AspNetPager
http://www.webdiyer.com/AspNetPagerDocs/index.html