GridView控件自定义分页详解

9/29/2006来源:ASP.NET技巧人气:36197

  前些天我写了关于 <<在存储过程中实现分页>>和<<GridView控件事件详解 >> ,后来又有一些人问我怎样在GridView中应用这个东东!其实很简单,主要是怎么保存当前页面的页码PageIndex问题,不过把这个解决了什么都好办了.因为在分页过程中:PageSize是一定的,我们可以用一个属性来表示.保存PageIndex好多中方法,而且数据不是很庞大,基本不会好太多的资源.还是一句老话,话再多都没有例子直观.

  在这里我们将用一个隐藏字段来保存这个PageIndex,即当前页码.当点击上一页时,将它的值减一,知道为0,要注意的一点这里的第一页页码是0而不是1.下面看看代码,然后我们再分析分析!
 1<asp:GridView ID="NewsGrid" runat="server" AutoGenerateColumns="False" AllowPaging="false" Width="100%">
 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>  CS文件中的代码:
 1        protected void PagerButton_Click(object sender, EventArgs e)
 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        } 
  是不是很简单啊,看了代码就都懂了,关于用按钮传参数在这里就不多说了,还有一个就是Entities属性,在<<应用实体类EntitySet实现类似的范型功能 >>这篇文章中可以找到
http://www.cnblogs.com/xdotnet/archive/2006/09/29/gridview_paging_myself.html