[ListView] 在 ListView 頁碼欄中顯示頁數與筆數

ListView 是 .Net 3.0 以後才出現的一個資料控制項, 也是一個綜合了包括 GridView, FormView 等各種 Data Container 功能與特色的新產品。ListView 可以搭配 DataPager 來控制其頁碼列, 如此, 對於 ListView 的跳頁控制, 增加了很大的彈性...

 

ListView 是 .Net 3.0 以後才出現的一個資料控制項, 也是一個綜合了包括 GridView, FormView 等各種 Data Container 功能與特色的新產品。ListView 可以搭配 DataPager 來控制其頁碼列, 如此, 對於 ListView 的跳頁控制, 增加了很大的彈性。

以下是一個 ListView 的範例; 我們先看看其 LayoutTemplate 的部份:

<LayoutTemplate>
<table runat="server" width="100%">
    <tr runat="server">
        <td runat="server">
            <table id="itemPlaceholderContainer" runat="server" border="2" width="100%"
                style='background-color: #FFFFFF; border-collapse: collapse; border-color: #999999;
                border-style: none; border-width: 1px; font-family: 微軟正黑體; font-size: small;
                table-layout: inherit">
                <tr id="Tr2" runat="server" style='background-color: #5B7D9F; color: #FFFFFF;
                    text-align: center; font-size: medium; font-weight: bold">
                    <th id="Th0" runat="server">
                        Select
                    </th>
                    <th id="Th1" runat="server">
                        EmployeeID
                    </th>
                    <th id="Th2" runat="server">
                        Name
                    </th>
                    <th id="Th3" runat="server">
                        Gender
                    </th>
                    <th id="Th4" runat="server">
                        Title
                    </th>
                    <th id="Th5" runat="server">
                        Company
                    </th>
                    <th id="Th6" runat="server">
                        Birthday
                    </th>
                    <th id="Th7" runat="server">
                        On Board
                    </th>
                    <th id="Th8" runat="server">
                        <asp:Label runat="server" ID="lbDeleteButton" Text="Delete" />
                    </th>
                </tr>
                <tr id="itemPlaceholder" runat="server">
                </tr>
            </table>
        </td>
    </tr>
    <tr id="Tr3" runat="server">
        <td id="Td2" runat="server" style='text-align: center; background-color: #CCCCCC; font-family: 微軟正黑體, Arial; color: #000000;'>
            <asp:DataPager ID="DataPager1" runat="server">
                <Fields>
                    <asp:NextPreviousPagerField ButtonType="Image" ShowFirstPageButton="True" FirstPageText="第一頁"
                        ShowNextPageButton="False" ShowPreviousPageButton="False" FirstPageImageUrl="~/Images/First.gif" />
                    <asp:NextPreviousPagerField ButtonType="Image" ShowNextPageButton="False" PreviousPageText="上一頁"
                        ShowPreviousPageButton="True" PreviousPageImageUrl="~/Images/Previous.gif" />
                    <asp:NumericPagerField ButtonCount="10" ButtonType="Button" />
                    <asp:NextPreviousPagerField ButtonType="Image" ShowNextPageButton="True" NextPageText="下一頁"
                        ShowPreviousPageButton="False" NextPageImageUrl="~/Images/Next.gif" />
                    <asp:NextPreviousPagerField ButtonType="Image" ShowLastPageButton="True" LastPageText="最後一頁"
                        ShowNextPageButton="False" ShowPreviousPageButton="False" LastPageImageUrl="~/Images/Last.gif" />
                </Fields>
            </asp:DataPager>
            合計
            <asp:Label runat="server" ID="lbCount" Text="0" />
            筆 共
            <asp:Label runat="server" ID="lbPages" Text="1" />
            頁
        </td>
    </tr>
</table>
</LayoutTemplate>

接著, 是其程式部份:

protected void lv_DataBound(object sender, EventArgs e)
{
        Label lbCount = (Label)lv.FindControl("lbCount");
        Label lbPages = (Label)lv.FindControl("lbPages");
        DataPager dp = (DataPager)lv.FindControl("DataPager1");
        if (dp != null)
        {
            if (lbCount != null) lbCount.Text = dp.TotalRowCount.ToString();
            int pageCount = (int)Math.Ceiling((double)dp.TotalRowCount / (double)dp.PageSize);
            if (lbPages != null) lbPages.Text = pageCount.ToString();
        }
}

程式邏輯很直接, 所以我不多加說明。基本上, 重點就在如何找到 DataPager, 再計算總共幾頁、總共幾筆的數字, 再填入 Label 控制項即可。


Dev 2Share @ 點部落