.NET分页之DataPager控件分页(假分页)

博客原文:http://blog.csdn.net/dongdong625/archive/2010/06/04/5647544.aspx

源码下载:http://down.qiannao.com/space/file/qiannao/share/2010/6/4/Paging.zip/.page

ASP.NET数据控件中提供了一个叫“DataPager”的控件用于分页,但是这个控件的用法却非常局限,只有实现了IPageableItemContainer接口才能使用此控件进行分页,b并且是假分页,ASP.NET3.5中也只有ListView控件实现了该接口。ListView控件是一个功能非常强大的控件,他基本上涵盖了其他所有数据绑定控件的功能,而且用法也稍有不同,他提供了很多模板,以其最常用的两个为例,在LayoutTemplate中布局,在需要其他模板的位置加上PlaceHolder占位控件,然后在其他模板中实现,例如:

     <LayoutTemplate>

            <table style="width:300px;">

                <thead>

                    <tr>

                        <th>编号</th>

                        <th>县市</th>

                        <th>市编号</th>

                    </tr>

                </thead>

                <tbody>

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

                </tbody>

            </table>

        </LayoutTemplate>

        <ItemTemplate>

            <tr>

                <td><%# Eval("o_id") %></td>

                <td><%# Eval("o_name") %></td>

                <td><%# Eval("c_id") %></td>

            </tr>

 </ItemTemplate>

DataPager控件主要有两个呈现项NextPreviousPagerField和NumericPagerField,前者是提供“上一页”、“下一页”等按钮,后者则提供“1”、“2”、“3”这种数字翻页的按钮。

DataPager有几个重要属性:

PagedControlID="ListView1" //被分页的控件

PageSize="10"              //每页显示的条数

TotalRowCount              //总条数(只读)

StartRowIndex              //当前页第一条记录在总记录中的索引(只读)

由这几个属性,则可以计算出

总页数= Math.Ceiling(pager.TotalRowCount * 1.0 / pager.PageSize)

当前页数=(pager.StartRowIndex / pager.PageSize) + 1

 

以下是示例源码:

<%@ Page Language="C#" AutoEventWireup="true" CodeFile="Default3.aspx.cs" Inherits="Default3" %>

 

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">

<html xmlns="http://www.w3.org/1999/xhtml">

<head runat="server">

    <title></title>

    <style type="text/css">

       .button

       {

           background-color:#CEEDFA;

           height:20px;

           border:solid 1px #3399FF;

       }

       body

       {

           color:#333333;

       }

    </style>

</head>

<body>

    <form id="form1" runat="server">

    <asp:ListView ID="ListView1" runat="server" DataSourceID="SqlDataSource1">

        <LayoutTemplate>

            <table style="width:300px;">

                <thead>

                    <tr>

                        <th>编号</th>

                        <th>县市</th>

                        <th>市编号</th>

                    </tr>

                </thead>

                <tbody>

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

                </tbody>

            </table>

        </LayoutTemplate>

        <ItemTemplate>

            <tr>

                <td><%# Eval("o_id") %></td>

                <td><%# Eval("o_name") %></td>

                <td><%# Eval("c_id") %></td>

            </tr>

        </ItemTemplate>

    </asp:ListView>

    <br />

    <table>

        <tr>

            <td>

                <%= string.Format("共<span style='color:#3399FF'>{0}</span>条记录 &nbsp;每页显示<span style='color:#3399FF'>{1}</span>条 &nbsp;共<span style='color:#3399FF'>{2}</span>页 &nbsp;当前第<span style='color:#3399FF'>{3}</span>页&nbsp;&nbsp;&nbsp;&nbsp;", pager.TotalRowCount, pager.PageSize, Math.Ceiling(pager.TotalRowCount * 1.0 / pager.PageSize), (pager.StartRowIndex / pager.PageSize) + 1)%>

            </td>

            <td>

                <asp:DataPager runat="server" ID="pager" PagedControlID="ListView1" PageSize="10">

                    <Fields>

                        <asp:NextPreviousPagerField FirstPageText="首页" PreviousPageText="上一页" NextPageText="下一页" ButtonType="Button" ButtonCssClass="button"

                            LastPageText="尾页" ShowFirstPageButton="true" ShowLastPageButton="true" ShowNextPageButton="true" ShowPreviousPageButton="true" />

                        <asp:NumericPagerField ButtonCount="5" NumericButtonCssClass="button" ButtonType="Button"  NextPreviousButtonCssClass="button"  />

                    </Fields>

                </asp:DataPager>

            </td>

        </tr>

    </table>

    <asp:SqlDataSource ID="SqlDataSource1" runat="server" ConnectionString="<%$ ConnectionStrings:connstr %>"

        SelectCommand="SELECT * FROM [County]">

    </asp:SqlDataSource>

    </form>

</body>

</html>

 

运行效果:

 

本文来自CSDN博客,转载请标明出处:http://blog.csdn.net/dongdong625/archive/2010/06/04/5647544.aspx

源码下载

 

posted @ 2010-10-21 14:33  唔愛吃蘋果  阅读(7102)  评论(0编辑  收藏  举报