今天做了一个分页的例子本以为没啥问题,但无意中被我发现了个郁闷的问题也不知道怎么回事,贴出来让大家看看。页面部分就一个DataList控件和几个导航条,后台代码:
![](https://i-blog.csdnimg.cn/blog_migrate/8f900a89c6347c561fdf2122f13be562.gif)
![ExpandedBlockStart.gif](https://i-blog.csdnimg.cn/blog_migrate/961ddebeb323a10fe0623af514929fc1.gif)
1
protected
void
Page_Load(
object
sender, EventArgs e)
2 {
3 if ( ! IsPostBack)
4 {
5 int CurrentPage = 1 ;
6 if (Request.QueryString[ " Page " ] != null && Request.QueryString[ " Page " ] != "" )
7 {
8 CurrentPage = Convert.ToInt32( Request.QueryString[ " Page " ]);
9 }
10 BindData(CurrentPage);
11 }
12
13 }
14
15 protected void BindData( int page)
16 {
17 DataSet ds = DbHelperSQL.Query( " select * from Products " );
18 PagedDataSource pds = new PagedDataSource();
19 int countRows = ds.Tables[ 0 ].Rows.Count;
20 int pageSize = 70 ;
21 int countPage = Convert.ToInt32(Math.Ceiling(( double )countRows / pageSize));
22 pds.DataSource = ds.Tables[ 0 ].DefaultView;
23 pds.AllowPaging = true ;
24 pds.PageSize = pageSize;
25 pds.CurrentPageIndex = page - 1 ;
26 DataList1.DataSource = pds;
27 DataList1.DataBind();
28 SetNavigate( pds,page);
29 Label1.Text = " <span style=\ " color:red\ " > " + page.ToString() + " </span>/ " + countPage.ToString() + " ,每页显示 " + pageSize.ToString() + " 记录,共 " +
30 countRows.ToString() + " 条记录! " ;
31 }
32
33 protected void SetNavigate(PagedDataSource pds, int page)
34 {
35 if (pds.PageCount > 1 )
36 {
37 if (pds.IsFirstPage)
38 {
39 lnkNext.NavigateUrl = " Default2.aspx?Page= " + Convert.ToString(page + 1 );
40 lnkLast.NavigateUrl = " Default2.aspx?Page= " + Convert.ToString(pds.PageCount);
41 }
42 else if (pds.IsLastPage)
43 {
44 lnkFirst.NavigateUrl = " Default2.aspx?Page=1 " ;
45 lnkPrev.NavigateUrl = " Default2.aspx?Page= " + Convert.ToString(page - 1 );
46 }
47 else
48 {
49 lnkFirst.NavigateUrl = " Default2.aspx?Page=1 " ;
50 lnkPrev.NavigateUrl = " Default2.aspx?Page= " + Convert.ToString(page - 1 );
51 lnkNext.NavigateUrl = " Default2.aspx?Page= " + Convert.ToString(page + 1 );
52 lnkLast.NavigateUrl = " Default2.aspx?Page= " + Convert.ToString(pds.PageCount);
53 }
54 }
55 }
2 {
3 if ( ! IsPostBack)
4 {
5 int CurrentPage = 1 ;
6 if (Request.QueryString[ " Page " ] != null && Request.QueryString[ " Page " ] != "" )
7 {
8 CurrentPage = Convert.ToInt32( Request.QueryString[ " Page " ]);
9 }
10 BindData(CurrentPage);
11 }
12
13 }
14
15 protected void BindData( int page)
16 {
17 DataSet ds = DbHelperSQL.Query( " select * from Products " );
18 PagedDataSource pds = new PagedDataSource();
19 int countRows = ds.Tables[ 0 ].Rows.Count;
20 int pageSize = 70 ;
21 int countPage = Convert.ToInt32(Math.Ceiling(( double )countRows / pageSize));
22 pds.DataSource = ds.Tables[ 0 ].DefaultView;
23 pds.AllowPaging = true ;
24 pds.PageSize = pageSize;
25 pds.CurrentPageIndex = page - 1 ;
26 DataList1.DataSource = pds;
27 DataList1.DataBind();
28 SetNavigate( pds,page);
29 Label1.Text = " <span style=\ " color:red\ " > " + page.ToString() + " </span>/ " + countPage.ToString() + " ,每页显示 " + pageSize.ToString() + " 记录,共 " +
30 countRows.ToString() + " 条记录! " ;
31 }
32
33 protected void SetNavigate(PagedDataSource pds, int page)
34 {
35 if (pds.PageCount > 1 )
36 {
37 if (pds.IsFirstPage)
38 {
39 lnkNext.NavigateUrl = " Default2.aspx?Page= " + Convert.ToString(page + 1 );
40 lnkLast.NavigateUrl = " Default2.aspx?Page= " + Convert.ToString(pds.PageCount);
41 }
42 else if (pds.IsLastPage)
43 {
44 lnkFirst.NavigateUrl = " Default2.aspx?Page=1 " ;
45 lnkPrev.NavigateUrl = " Default2.aspx?Page= " + Convert.ToString(page - 1 );
46 }
47 else
48 {
49 lnkFirst.NavigateUrl = " Default2.aspx?Page=1 " ;
50 lnkPrev.NavigateUrl = " Default2.aspx?Page= " + Convert.ToString(page - 1 );
51 lnkNext.NavigateUrl = " Default2.aspx?Page= " + Convert.ToString(page + 1 );
52 lnkLast.NavigateUrl = " Default2.aspx?Page= " + Convert.ToString(pds.PageCount);
53 }
54 }
55 }
第一页没问题
第二页咋看之下似乎也没啥问题
奇怪就奇怪在第二页
当我把鼠标稍微移动到尾页的右侧竟然有个链接,还是返回到首页的真是很奇怪,不知道谁遇到过!