GridView和Repeater自定義分頁功能的實現

1、GridView自定義分頁前臺核心代碼:


GridView.aspx


...


<PagerTemplate>

                <br />

                <asp:Label ID="lblPage" runat="server" Text="<%# "共" + ((GridView)Container.NamingContainer).PageCount + "頁,當前第" + (((GridView)Container.NamingContainer).PageIndex + 1) + "頁" %>"></asp:Label>

                <asp:LinkButton ID="lbtnFirst" runat="server" Text="首頁" Enabled="<%# ((GridView)Container.NamingContainer).PageIndex != 0 %>"

                    CommandName="Page" CommandArgument="First"></asp:LinkButton>

                <asp:LinkButton ID="lbtnPrev" runat="server" Text="上一頁" Enabled="<%# ((GridView)Container.NamingContainer).PageIndex != 0 %>"

                    CommandName="Page" CommandArgument="Prev"></asp:LinkButton>

                <asp:PlaceHolder ID="numeric" runat="server"></asp:PlaceHolder>

                <asp:LinkButton ID="lbtnNext" runat="server" Text="下一頁" Enabled="<%# ((GridView)Container.NamingContainer).PageIndex != (((GridView)Container.NamingContainer).PageCount - 1) %>"

                    CommandName="Page" CommandArgument="Next"></asp:LinkButton>

                <asp:LinkButton ID="lbtnLast" runat="server" Text="尾頁" Enabled="<%# ((GridView)Container.NamingContainer).PageIndex != (((GridView)Container.NamingContainer).PageCount - 1) %>"

                    CommandName="Page" CommandArgument="Last"></asp:LinkButton>

                <asp:DropDownList ID="ddlJumpPage" runat="server">

                </asp:DropDownList>

                <asp:ImageButton ID="btnGo" runat="server" Width="30px" Height="20px" ImageAlign="Bottom" ImageUrl="~/images/go.jpg" CommandName="Go" />

            </PagerTemplate>

 

2、GridView自定義分頁後臺核心代碼:


GridView.aspx.cs


//根據命令來變更當前頁碼

    protected void gvTest_RowCommand(object sender, GridViewCommandEventArgs e)

    {

        if (e.CommandName == "Page")

        {

            switch (e.CommandArgument.ToString())

            {

                case "First":

                    gvTest.PageIndex = 0;

                    break;

                case "Prev":

                    if (gvTest.PageIndex > 0)

                    {

                        gvTest.PageIndex -= 1;

                    }

                    break;

                case "Next":

                    if (gvTest.PageIndex < (gvTest.PageCount - 1))

                    {

                        gvTest.PageIndex += 1;

                    }

                    break;

                case "Last":

                    gvTest.PageIndex = gvTest.PageCount - 1;

                    break;

                default:

                    gvTest.PageIndex = Int32.Parse(e.CommandArgument.ToString()) - 1;

                    break;

            }

        }

        else

        {

            if (e.CommandName == "Go")

            {

                DropDownList ddlJumpPage = (DropDownList)gvTest.BottomPagerRow.FindControl("ddlJumpPage");

                int index = Int32.Parse(ddlJumpPage.SelectedValue);

                gvTest.PageIndex = index - 1;

                GetData();

            }

        }

    }



    //顯示頁碼導航

    protected void gvTest_RowCreated(object sender, GridViewRowEventArgs e)

    {

        if (e.Row.RowType == DataControlRowType.Pager)

        {

            PlaceHolder ph = (PlaceHolder)e.Row.FindControl("numeric");

            int startIndex = (gvTest.PageIndex) / 10 * 10 + 1;//根據當前頁得到第一個頁碼

            int endIndex = startIndex + 9;//顯示10個頁碼

            if (endIndex > gvTest.PageCount)//判斷是否爲最後一頁

            {

                endIndex = gvTest.PageCount;

            }

            for (int i = startIndex; i <= endIndex; i++)//通過循環動態顯示頁碼

            {

                var lbtn = new LinkButton { CommandName = "Page", CommandArgument = i.ToString(), Text = "[" + i + "]" };

                ph.Controls.Add(i - 1 != gvTest.PageIndex ? (Control)lbtn : new LiteralControl(i.ToString()));

                ph.Controls.Add(new LiteralControl(" "));

            }

        }

    }

 

3、Repeater自定義分頁前臺核心代碼


Repeater.aspx


...


<asp:Repeater ID="rpTest" runat="server" OnItemDataBound="rpTest_ItemDataBound" OnItemCreated="rpTest_ItemCreated">



...



<FooterTemplate>

                <tr>

                    <td colspan="6">

                        <br />

                        共<asp:Label ID="lblPageCount" runat="server" Text=""></asp:Label>頁,當前第<asp:Label

                            ID="lblCurrentPage" runat="server" Text=""></asp:Label>頁

                        <asp:HyperLink ID="hlFirst" runat="server" Text="首頁"></asp:HyperLink>

                        <asp:HyperLink ID="hlPrev" runat="server" Text="上一頁"></asp:HyperLink>

                        <asp:PlaceHolder ID="numeric" runat="server"></asp:PlaceHolder>

                        <asp:HyperLink ID="hlNext" runat="server" Text="下一頁"></asp:HyperLink>

                        <asp:HyperLink ID="hlLast" runat="server" Text="尾頁"></asp:HyperLink>

                        <asp:DropDownList ID="ddlJumpPage" runat="server">

                        </asp:DropDownList>

                        <asp:ImageButton ID="ibtnGo" runat="server" Width="30px" Height="20px" ImageAlign="Bottom"

                            ImageUrl="~/images/go.jpg" OnClick="ibtnGo_Click" />

                    </td>

                </tr>

                </table>

            </FooterTemplate>

</asp:Repeater>

<asp:Label ID="lblMessage" runat="server"></asp:Label>


 


4、Repeater自定義分頁後臺核心代碼


Repeater.aspx.cs


//返回待綁定的數據源

    private PagedDataSource GetPds()

    {

        StudentInfo stuInfo = new StudentInfo();

        DataSet ds = stuInfo.GetStudent();

        PagedDataSource pds = new PagedDataSource();

        pds.DataSource = ds.Tables[0].DefaultView;//設置數據源

        pds.AllowPaging = true;//允許分頁

        pds.PageSize = 10;//一頁顯示10條信息

        if (Request.QueryString["page"] != null)//判斷頁面傳值是否爲空

        {

            pds.CurrentPageIndex = Int32.Parse(Request.QueryString["page"].ToString()) - 1;

        }

        else

        {

            pds.CurrentPageIndex = 0;

        }

        return pds;

    }



 



//顯示底部分頁按鈕並向下拉列表控件中填充數據

    protected void rpTest_ItemDataBound(object sender, RepeaterItemEventArgs e)

    {

        if (GetPds().DataSourceCount != 0)

        {

            if (e.Item.ItemType == ListItemType.Footer)//判斷是否爲頁腳

            {

                Label lblRecordCount = (Label)e.Item.FindControl("lblRecordCount");

                Label lblPageCount = (Label)e.Item.FindControl("lblPageCount");

                Label lblCurrentPage = (Label)e.Item.FindControl("lblCurrentPage");

                HyperLink hlFirst = (HyperLink)e.Item.FindControl("hlFirst");

                HyperLink hlPrev = (HyperLink)e.Item.FindControl("hlPrev");

                HyperLink hlNext = (HyperLink)e.Item.FindControl("hlNext");

                HyperLink hlLast = (HyperLink)e.Item.FindControl("hlLast");

                DropDownList ddlJumpPage = (DropDownList)e.Item.FindControl("ddlJumpPage");

                int pageCount = GetPds().PageCount;

                int curPage = GetPds().CurrentPageIndex + 1;

                for (int i = 1; i <= pageCount; i++)

                {

                    ddlJumpPage.Items.Add(i.ToString());

                }

                if (pageCount == 1)//若總頁數只有一頁,則底部分頁鏈接不可用

                {

                    hlFirst.Enabled = false;

                    hlPrev.Enabled = false;

                    hlNext.Enabled = false;

                    hlLast.Enabled = false;

                }

                else

                {

                    if (curPage <= 1)//若當前爲首頁,則首頁和上一頁的鏈接不可用

                    {

                        hlFirst.Enabled = false;

                        hlPrev.Enabled = false;

                        hlNext.Enabled = true;

                        hlLast.Enabled = true;

                    }

                    else

                    {

                        hlPrev.NavigateUrl = "?page=" + (curPage - 1);

                    }

                    if (curPage >= pageCount)//若當前頁爲最後一頁,則下一頁和尾頁的鏈接不可用

                    {

                        hlFirst.Enabled = true;

                        hlPrev.Enabled = true;

                        hlNext.Enabled = false;

                        hlLast.Enabled = false;

                    }

                    else

                    {

                        hlNext.NavigateUrl = "?page=" + (curPage + 1);

                    }

                    hlFirst.NavigateUrl = "?page=1";

                    hlLast.NavigateUrl = "?page=" + pageCount;

                }

                lblPageCount.Text = pageCount.ToString();

                lblCurrentPage.Text = curPage.ToString();

            }

        }

        else//若數據源中無信息,則顯示相關提示

        {

            rpTest.Visible = false;

            lblMessage.Text = "無相關記錄";

        }

    }



 



//顯示頁碼導航

    protected void rpTest_ItemCreated(object sender, RepeaterItemEventArgs e)

    {

        if (GetPds().DataSourceCount != 0)

        {

            if (e.Item.ItemType == ListItemType.Footer)

            {

                PlaceHolder ph = (PlaceHolder)e.Item.FindControl("numeric");

                int pageCount = GetPds().PageCount;

                int curPage = GetPds().CurrentPageIndex + 1;

                int startIndex = (curPage - 1) / 10 * 10 + 1;//根據當前頁得到第一個頁碼

                int endIndex = startIndex + 9;//顯示10個頁碼

                if (endIndex > pageCount)//判斷是否爲最後一頁

                {

                    endIndex = pageCount;

                }

                for (int i = startIndex; i <= endIndex; i++)//通過循環動態顯示頁碼

                {

                    var hl = new HyperLink { ID = "hl" + i, Text = "[" + i + "]", NavigateUrl = "?page=" + i };

                    ph.Controls.Add(i != curPage ? (Control)hl : new LiteralControl(i.ToString()));

                    ph.Controls.Add(new LiteralControl(" "));

                }

            }

        }

    }




 


發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章