GridView 自定义分页

<PagerTemplate>
                            <table>
                                <tr>
                                    <td>
                        <asp:Label  ID="LabelCurrentPage" runat="server"
 >当前页:<%# ((GridView)Container.NamingContainer).PageIndex + 1 %></asp:Label></td>
                                    <td>
 <asp:Label ID="LabelPageCount" runat="server" >总页数:<%# ((GridView)Container.NamingContainer).PageCount %></asp:Label></td>
                                    <td>
 <asp:LinkButton ID="LinkButtonFirstPage" runat="server" CommandArgument="First" CommandName="Page"
 Enable="<%# ((GridView)Container.NamingContainer).PageIndex != 0 %>">首页</asp:LinkButton></td>
                                    <td>

<asp:LinkButton ID="LinkButtonPreviousPage" runat="server" CommandArgument="Prev" CommandName="Page"
 Enable="<%# ((GridView)Container.NamingContainer).PageIndex != 0 %>">上一页</asp:LinkButton></td>
                                    <td>

<asp:LinkButton ID="LinkButtonNextPage" runat="server" CommandArgument="Next" CommandName="Page"
 Enable="<%# ((GridView)Container.NamingContainer).PageIndex != ((GridView)Container.NamingContainer).PageCount - 1 %>">下一页</asp:LinkButton></td>
 <td>

<asp:LinkButton ID="LinkButtonLastPage" runat="server" CommandArgument="Last" CommandName="Page"
 Enable="<%# ((GridView)Container.NamingContainer).PageIndex != ((GridView)Container.NamingContainer).PageCount - 1 %>">尾页</asp:LinkButton></td>
                                </tr>
                            </table>
                        </PagerTemplate> 
把以上表格放到<PagerTemplate>即可


ASP.NET 2.0 种提供了 GridView 控件。该控件的分页比较方便,可以通过在 Visual Studio .NET 2005 种简单设置即可实现各种分页功能。

1.  默认分页方式
(1) 
是否允许分页
GridView AllowPaging 属性。

(2) 
每页记录数
GridView PageSize

(3) 
分页导航条形式
GridView PagerSettings 属性的 Mode Numeric NextPrevious NextPreviousFirstLast NumericFirstLast

2.  自定义分页
( 1)  当前页
< asp:Label   ID ="LabelCurrentPage"  runat ="server"  
 Text
="<%# ((GridView)Container.NamingContainer).PageIndex + 1 %>"></ asp:Label > 


(2)  总页数
< asp:Label  ID ="LabelPageCount"  runat ="server"  
 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 > 
<asp:textbox id="txtNewPageIndex" runat="server" width="20px" text='<%# ((GridView)Container.Parent.Parent).PageIndex + 1 %>' />
<asp:linkbutton id="btnGo" runat="server" causesvalidation="False" commandargument="-1" commandname="Page" text="GO" />
 
: 将上述代码放在 GridView <PagerTemplate></PagerTemplate>
 
PageIndexChanging 事件中加入如下代码
    protected void GridView1_PageIndexChanging(object sender, GridViewPageEventArgs e)
    {
       GridView theGrid = sender as GridView// refer to the GridView
        int newPageIndex = 0;
        if (-2 == e.NewPageIndex) { // when click the "GO" Button
         TextBox txtNewPageIndex = null;
 //GridViewRow pagerRow = theGrid.Controls[0].Controls[theGrid.Controls[0].Controls.Count - 1] as GridViewRow; // refer to PagerTemplate
GridViewRow pagerRow = theGrid.BottomPagerRow; //GridView 较DataGrid提供了更多的API,获取分页块可以使用BottomPagerRow 或者TopPagerRow,当然还增加了HeaderRow和FooterRow
//updated at 2006 年月日:15:33
            if (null != pagerRow) {
                txtNewPageIndex = pagerRow.FindControl("txtNewPageIndex") as TextBox;   // refer to the TextBox with the NewPageIndex value
            }
            if (null != txtNewPageIndex) {
                newPageIndex = int.Parse(txtNewPageIndex.Text) - 1; // get the NewPageIndex
            }
        }
        else// when click the first, last, previous and next Button
            newPageIndex = e.NewPageIndex;
        }
        // check to prevent form the NewPageIndex out of the range
        newPageIndex = newPageIndex < 0 ? 0 : newPageIndex;
        newPageIndex = newPageIndex >= theGrid.PageCount ? theGrid.PageCount - 1 : newPageIndex;
        // specify the NewPageIndex
        theGrid.PageIndex = newPageIndex;
        // rebind the control
        // in this case of retrieving the data using the xxxDataSoucr control,
        // just do nothing, because the asp.net engine binds the data automatically
    }
  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 5
    评论
评论 5
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值