1. 默认分页方式
(1) 是否允许分页
GridView的AllowPaging属性。
(2) 每页记录数
GridView的PageSize
(3) 分页导航条形式
GridView的PagerSettings属性的Mode:Numeric,NextPrevious,NextPreviousFirstLast,NumericFirstLast。
2. 自定义分页
(1) 当前页
Text ="<%# ((GridView)Container.NamingContainer).PageIndex + 1 %>" ></ asp:Label >
(2) 总页数
Text ="<%# ((GridView)Container.NamingContainer).PageCount %>" ></ asp:Label >
(3) 首页、上一页、下一页、尾页
<asp:LinkButton ID="LinkButtonFirstPage" runat="server" CommandArgument="First" CommandName="Page"
Visible="<%# ((GridView)Container.NamingContainer).PageIndex != 0 %>">首页</asp:LinkButton>
<asp:LinkButton ID="LinkButtonPreviousPage" runat="server" CommandArgument="Prev" CommandName="Page"
Visible="<%# ((GridView)Container.NamingContainer).PageIndex != 0 %>">上一页</asp:LinkButton>
<asp:LinkButton ID="LinkButtonNextPage" runat="server" CommandArgument="Next" CommandName="Page"
Visible="<%# ((GridView)Container.NamingContainer).PageIndex != ((GridView)Container.NamingContainer).PageCount - 1 %>">下一页</asp:LinkButton>
<asp:LinkButton ID="LinkButtonLastPage" runat="server" CommandArgument="Last" CommandName="Page"
Visible="<%# ((GridView)Container.NamingContainer).PageIndex != ((GridView)Container.NamingContainer).PageCount - 1 %>">尾页</asp:LinkButton>
转自:
http://hi.baidu.com/%CF%F2%CD%F9%BC%BE%BA%F3%C8%FC/blog/item/5997ddc9771cd5107e3e6f64.html
前些天我写了关于 <<在存储过程中实现分页>>和<<GridView控件事件详解 >> , 后来又有一些人问我怎样在GridView中应用这个东东!其实很简单,主要是怎么保存当前页面的页码PageIndex问题,不过把这个解决了什么都好 办了.因为在分页过程中:PageSize是一定的,我们可以用一个属性来表示.保存PageIndex好多中方法,而且数据不是很庞大,基本不会好太多 的资源.还是一句老话,话再多都没有例子直观.
在这里我们将用一个隐藏字段来保存这个PageIndex,即当前页码.当点击上一页时,将它的值减一,知道为0,要注意的一点这里的第一页页码是0而不是1.下面看看代码,然后我们再分析分析!
2 < Columns >
3 < asp:BoundField DataField ="NewsId" HeaderText ="新闻ID" />
4 < asp:HyperLinkField DataNavigateUrlFields ="NewsId" DataNavigateUrlFormatString ="~/Details.aspx?ID={0}"
5 DataTextField ="Title" HeaderText ="新闻标题" ItemStyle-Width ="70%" />
6 < asp:BoundField DataField ="PostTime" HeaderText ="发布时间" />
7 < asp:CommandField HeaderText ="新闻管理" ShowCancelButton ="False" ShowDeleteButton ="True"
8 ShowEditButton ="True" />
9 </ Columns >
10 </ asp:GridView >
11 < div style =" height:16px; padding-top:5px; margin-right:30px; float:right" >
12 < asp:HiddenField ID ="CurrentPage" runat ="server" Value ="0" />
13 < asp:LinkButton ID ="First" runat ="server" CommandArgument ="first" OnClick ="PagerButton_Click" > 首 页 </ asp:LinkButton >
14 < asp:LinkButton ID ="Prev" runat ="server" CommandArgument ="prev" OnClick ="PagerButton_Click" > 上一页 </ asp:LinkButton >
15 < asp:LinkButton ID ="Next" runat ="server" CommandArgument ="next" OnClick ="PagerButton_Click" > 下一页 </ asp:LinkButton >
16 < asp:LinkButton ID ="Last" runat ="server" CommandArgument ="last" OnClick ="PagerButton_Click" > 尾 页 </ asp:LinkButton >
17 </ div >
2 {
3 int pageIndx = Convert.ToInt32(CurrentPage.Value);
4 int totals = NewsManager.GetNews(0, pageSize).TotalRecords;
5 int pages = (totals % pageSize) == 0 ? (totals / pageSize) : (totals / pageSize + 1);
6 string arg = ((LinkButton)sender).CommandArgument.ToString().ToLower();
7 switch (arg)
8 {
9 case "prev":
10 if (pageIndx > 0)
11 {
12 pageIndx -= 1;
13 }
14 break;
15 case "next":
16 if (pageIndx < pages - 1)
17 {
18 pageIndx += 1;
19 }
20 break;
21 case "last":
22 pageIndx = pages - 1;
23 break;
24 default:
25 pageIndx = 0;
26 break;
27 }
28 CurrentPage.Value = pageIndx.ToString();
29 NewsGrid.DataSource = NewsManager.GetNews(pageIndx , pageSize).Entities;
30 NewsGrid.DataBind();
31 }