GridView分頁使用

來源:互聯網
上載者:User

標籤:style   blog   http   color   使用   資料   

GridView控制項內建分頁,綁定資料來源控制項後allowPaging即可自動實現分頁。此篇為代碼實現分頁方法。

 

PageLoad事件中指定允許分頁和每頁個數

GridView2.AllowPaging = true;GridView2.PageSize = 5;

當需要使用自己的翻頁介面布局GridView提供了編輯模板使用。在模板選擇的下拉框中選中PagerTemplate即可自訂布局

<asp:GridView ID="GridView2" runat="server" AutoGenerateColumns="False" OnPageIndexChanging="GridView2_PageIndexChanging"        AllowPaging="true" PageSize="2" PageIndex="1">                <Columns>            <asp:BoundField DataField="ID" HeaderText="ID" />            <asp:BoundField DataField="Name" HeaderText="姓名" />            <asp:BoundField DataField="Code" HeaderText="編碼" />            <asp:BoundField DataField="Level" HeaderText="等級" />        </Columns>        <PagerTemplate>            當前第:            <%--((GridView)Container.NamingContainer)就是為了得到當前的GridView控制項--%>            <asp:Label ID="LabelCurrentPage" runat="server" Text="<%# ((GridView)Container.NamingContainer).PageIndex + 1 %>"></asp:Label>            頁/共:            <asp:Label ID="LabelPageCount" runat="server" Text="<%# ((GridView)Container.NamingContainer).PageCount %>"></asp:Label>            頁            <%--  分頁是首分頁時,首頁按鈕不顯示;上一頁,下一頁;分頁是尾頁,則尾頁按鈕不顯示                CommandArgument通常只在設定CommandName屬性時使用,作為指定補充 CommandName 屬性的參數,提供要執行的Command 事件處理常式的選擇性參數。例如 CommandName 屬性設定為 Sort 並將 CommandArgument 屬性設定為 Ascending,以指定按升序排序的命令。                             --%>                         <%-- 首頁按鈕,對應了內建識別的命令參數CommandArgument--%>            <asp:LinkButton ID="LinkButtonFirstPage" runat="server" CommandArgument="First" CommandName="Page"                Visible=‘<%# GridView2.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" />頁            <%--這裡將CommandArgument即使點擊該按鈕e.newIndex 值為3 --%>            <asp:LinkButton ID="btnGo" runat="server" CausesValidation="False" CommandArgument="-1"                CommandName="Page" Text="GO" />        </PagerTemplate>    </asp:GridView>
View Code

其中代碼

(GridView)Container.NamingContainer

 

 

用於在GridView的PagerTemplate中擷取到GridView,以PageIndex屬性判斷按鈕的顯示和隱藏。

 

後台操作只需在PageIndexChanging事件中進行處理。

   private void Bind(string sqlStr)    {          SqlData SqlData = new SqlData();        DataTable dt = SqlData.GetDataTableFromDB(sqlStr);        GridView2.DataSource = dt;        GridView2.DataBind();    }    

SqlData類為資料庫連接類,擷取一張資料表

 

protected void GridView2_PageIndexChanging(object sender, GridViewPageEventArgs e)    {                                        int newPageIndex = 0;        if (e.NewPageIndex < 0)        {            //點擊了Go按鈕            TextBox txtNewPageIndex = null;            //GridView較DataGrid提供了更多的API,擷取分頁塊可以使用BottomPagerRow 或者TopPagerRow,當然還增加了HeaderRow和FooterRow            GridViewRow pagerRow = GridView2.BottomPagerRow;            if (pagerRow != null)            {                //得到text控制項                txtNewPageIndex = pagerRow.FindControl("txtNewPageIndex") as TextBox;            }            if (txtNewPageIndex != null)            {                //得到索引                newPageIndex = int.Parse(txtNewPageIndex.Text) - 1;            }                    }        else        {            //點擊了其他的按鈕            newPageIndex = e.NewPageIndex;        }        //防止新索引溢出        newPageIndex = newPageIndex < 0 ? 0 : newPageIndex;        newPageIndex = newPageIndex >= GridView2.PageCount ? GridView2.PageCount - 1 : newPageIndex;        //得到新的值        GridView2.PageIndex = newPageIndex;        //重新綁定        string sqlStr = "SELECT [ID], [Name], [Code], [Level] FROM [Addr_OfficialCity]";        this.Bind(sqlStr);    }
View Code

 

 

即可完成分頁,簡潔明了

 

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.