关于AspNetPager 采用URL分页时 执行两次绑定的解决办法
2010-08-19 11:59 彭白洋 阅读(774) 评论(0) 编辑 收藏 举报先看代码:
1 <abw:AspNetPager ID="AspNetPager1" class="pager" runat="server"
2 AlwaysShow="true" PageSize="2" OnPageChanged="AspNetPager1_PageChanged"
3 UrlPaging="True" NextPageText="Next&gt;&gt;" PrevPageText="&lt;&lt;Prev"
4 ShowFirstLast="False" ShowPageIndexBox="Never"
5 CurrentPageButtonStyle="margin-right:2px;" PagingButtonSpacing="">
6 </abw:AspNetPager>
1 using System;
2 using System.Collections.Generic;
3 using System.Web.UI;
4 using AutoBuildWeb.Service;
5
6 namespace AutoBuildWeb.Web.Manage.WebData.ArticleManage
7 {
8 public partial class News : WebBase.Data
9 {
10 protected void Page_Load(object sender, EventArgs e)
11 {
12 if (!IsPostBack)
13 {
14 ArticlesInit();
15 }
16 }
17
18 /// <summary>
19 /// 初始化文章列表
20 /// </summary>
21 private void ArticlesInit()
22 {
23 int recordsCount;
24 string category = Request.QueryString["category"], key = Request.QueryString["key"];
25 int categoryId = string.IsNullOrEmpty(category) ? 0 : Convert.ToInt32(category);
26
27 if (!string.IsNullOrEmpty(key))
28 {
29 txtKeywords.Text = key;
30 }
31
32 IList<Object[]> list = ArticleSrv.GetArticles(categoryId, key, AspNetPager1.PageSize,
33 AspNetPager1.CurrentPageIndex, false, out recordsCount);
34
35 AspNetPager1.RecordCount = recordsCount;
36 rptArticle.DataSource = list;
37 rptArticle.DataBind();
38 }
39
40 protected void AspNetPager1_PageChanged(object sender, EventArgs e)
41 {
42 ArticlesInit();
43 }
44 }
45 }
46
在Page_Load 和 AspNetPager 的OnPageChanged 的事件中都绑定的话,会执行两次查询。
原因是 采用URL分页时 在页面加载时AspNetPager会自动执行 OnPageChanged事件
解决方法:
一、 首先在Page_Load 中获取数据数 用于初始化AspNetPager,然后仅在AspNetPager 的OnPageChanged 的事件中进行数据绑定就可以了,
详细: http://www.cnblogs.com/Leo_wl/archive/2010/06/22/1762966.html
二、大多数分页存储过程都是数据和统计一起返回到,所以第一种方法比较麻烦,经过测试发现了第二种方法。
获取page参数并在绑定代码中初始化AspNetPager的CurrentPageIndex,数据绑定操作只需要在Page_Load或者AspNetPager 的OnPageChanged中绑定一次就可以了,
上边的代码中的page参数是在基类WebBase.Data中获取赋给一个CurrentPageIndex 属性
修改后
1 <abw:AspNetPager ID="AspNetPager1" class="pager" runat="server"
2 AlwaysShow="true" PageSize="2"
3 UrlPaging="True" NextPageText="Next&gt;&gt;" PrevPageText="&lt;&lt;Prev"
4 ShowFirstLast="False" ShowPageIndexBox="Never"
5 CurrentPageButtonStyle="margin-right:2px;" PagingButtonSpacing="">
6 </abw:AspNetPager>
1 using System;
2 using System.Collections.Generic;
3 using System.Web.UI;
4 using AutoBuildWeb.Service;
5
6 namespace AutoBuildWeb.Web.Manage.WebData.ArticleManage
7 {
8 public partial class News : WebBase.Data
9 {
10 protected void Page_Load(object sender, EventArgs e)
11 {
12 if (!IsPostBack)
13 {
14 ArticlesInit();
15 }
16 }
17
18 /// <summary>
19 /// 初始化文章列表
20 /// </summary>
21 private void ArticlesInit()
22 {
23 int recordsCount;
24 string category = Request.QueryString["category"], key = Request.QueryString["key"];
25 int categoryId = string.IsNullOrEmpty(category) ? 0 : Convert.ToInt32(category);
26
27 if (!string.IsNullOrEmpty(key))
28 {
29 txtKeywords.Text = key;
30 }
31
32 IList<Object[]> list = ArticleSrv.GetArticles(categoryId, key, AspNetPager1.PageSize,
33 CurrentPageIndex, false, out recordsCount);
34
35 AspNetPager1.RecordCount = recordsCount;
36 AspNetPager1.CurrentPageIndex = CurrentPageIndex;
37 rptArticle.DataSource = list;
38 rptArticle.DataBind();
39 }
40 }
41 }
42