GridView自定义分页

来源:互联网 发布:网络机房设备 编辑:程序博客网 时间:2024/06/11 13:39
 
ASP.NET 2.0种提供了GridView控件。该控件的分页比较方便,可以通过在Visual Studio .NET 2005种简单设置即可实现各种分页功能。

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

(2) 
每页记录数
GridViewPageSize

(3) 
分页导航条形式
GridViewPagerSettings属性的ModeNumericNextPreviousNextPreviousFirstLastNumericFirstLast

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
    }
原创粉丝点击