ASP.NET 一个非常简单实用的分页方法

分页,也是一个非常常见得需求,以前也用过很多的分页控件,现在自己参照之前用过的,自己总结一个非常简单实用的分页实现方法。具体如下:数据展示用Repeater,分页的话用js切换当前的页码,后台根据当前页码获取数据绑定到Repeater。

数据列表代码

<div class="js_celli list">
    <div class="idea_cell_wh" style="overflow: visible">
        <table class="table table-striped table-hover">
            <asp:Repeater ID="SDRepeater" runat="server">
                <HeaderTemplate>
                    <tr class="tips_title">
                        <th>主题</th>
                        <th width="10%">处理状态</th>
                        <th width="10%">发帖人</th>
                        <th width="10%">时间</th>
                    </tr>
                </HeaderTemplate>
                <ItemTemplate>
                    <tr>
                        <td class="titletd" title="<%#Eval("Title") %>"><%#Eval("Title") %></td>
                        <td><%#Eval("Status")+"" == "" ? "未分派" : Eval("Status") %></td>
                        <td><%#Eval("Author") %></td>
                        <td><%#DateTime.Parse( Eval("Created")+"").ToString("yyyy-MM-dd hh:mm") %></td>
                    </tr>
                </ItemTemplate>
            </asp:Repeater>
        </table>
    </div>
    <div style="width: 100%; text-align: center;font-size:18px;color:#ffd800;font-weight:600;" ID="lbMessage" runat="server">
    </div>
</div>

分页HTML

<div class="container" style="text-align: center;">
    <div class="col-xs-12 col-sm-12 col-md-12 col-lg-12" style="margin: 0;">
        <ul id="PagerID" class="pagination pagination-sm">
            <li><a onclick="TurnPage('f')">首页 </a></li>
            <li><a onclick="TurnPage('p')">上页 </a></li>
            <li>
                <span>页:
                                    <asp:Label ID="lbCurrentPage" runat="server">0</asp:Label>/<asp:Label ID="lbMaxPage" runat="server">0</asp:Label>
                </span>
            </li>
            <li><a onclick="TurnPage('n')">下页 </a></li>
            <li><a onclick="TurnPage('e')">末页 </a></li>
            <li><a onclick="TurnPage('a')">GoTo </a></li>
            <li>
                <input type="text" id="txtInputNumber" style="width: 50px;"></li>
        </ul>
    </div>
</div>

分页JS

    function TurnPage(type) {
        var currentPage = getQueryString("currentPage") == null ? 1 : parseInt(getQueryString("currentPage"));
        var maxPage = <%=maxPage%>;

        switch (type) {
            case 'f':
                currentPage = 1;
                break;
            case 'p':
                if (currentPage <= 1) {
                    return;
                }
                else {
                    currentPage = currentPage - 1;
                }
                break;
            case 'n':
                if (currentPage >= maxPage) {
                    return;
                }
                else {
                    currentPage = currentPage + 1;
                }
                break;
            case 'e':
                if(currentPage == maxPage)return;
                currentPage = maxPage;
                break;
            case 'a':
                if ( isNaN(parseInt(document.getElementById("txtInputNumber").value))) {
                    return;
                } else if (parseInt(document.getElementById("txtInputNumber").value) > maxPage) {
                    alert("没有那么多页");
                    return;
                } else {
                    currentPage = parseInt(document.getElementById("txtInputNumber").value);
                }
                break;
            default:
                break;
        }

        if(!isNaN(currentPage) && currentPage > 0)
        {
            location.href = "?currentPage=" + currentPage;
        }
    }

分页样式

ul.pagination {
    display: inline-block;
}

.pagerlist {
    float: left;
    margin-left: 40%;
}

.pagination {
    display: inline-block;
    padding-left: 0;
    margin: 20px 0;
    border-radius: 4px;
}

.pagination {
    display: inline-block;
    padding-left: 0;
    margin: 20px 0;
    border-radius: 4px;
}

ul.pagination li {
    float: left;
    margin-right: 5px;
    border: #e2e2e2 1px solid;
}

.pagination > li {
    display: inline;
    cursor: pointer;
}

    .pagination > li:first-child > a, .pagination > li:first-child > span {
        margin-left: 0;
        border-top-left-radius: 4px;
        border-bottom-left-radius: 4px;
    }

ul.pagination li a, ul.pagination li a:link, ul.pagination li a:visited {
    display: block;
    padding: 0 10px;
    line-height: 24px;
    color: #666;
    font-size: 12px;
    text-decoration: none;
}

.pagination > li > a, .pagination > li > span {
    position: relative;
    float: left;
    padding: 2px 12px;
    margin-left: -1px;
    line-height: 1.42857143;
    color: #428bca;
    text-decoration: none;
    background-color: #fff;
    border: 1px solid #ddd;
}

后台代码

        //最大页码
        public int maxPage = 0;
        //页数
        int PageSize = 50;
        public SPWeb web = null;
        public bool IsCoordinator = false;

        protected void Page_Load(object sender, EventArgs e)
        {
            web = SPContext.Current.Web;

            int currentPage = 1;
            if (int.TryParse(Request["currentPage"] + "", out currentPage))
            {
            }

            if (currentPage < 1) currentPage = 1;

            DataTable dt = GetSDData();
            if (dt != null && dt.Rows.Count > 0)
            {
                lbMessage.Disabled = false;
                maxPage = (int)Math.Ceiling(dt.Rows.Count / (double)PageSize);

                lbCurrentPage.Text = currentPage + "";
                lbMaxPage.Text = maxPage + "";

                SDRepeater.DataSource = Common.GetPagedTable(dt, currentPage, PageSize);
                SDRepeater.DataBind();
            }
            else
            {
                lbMessage.Disabled = true;
                lbMessage.InnerText = " ----- no data ----- ";
            }

        }

获取当前页的数据

        /// <summary>
        /// DataTable分页
        /// </summary>
        /// <param name="dt">DataTable</param>
        /// <param name="PageIndex">PageIndex表示第几页</param>
        /// <param name="PageSize">PageSize表示每页的记录数</param>
        /// <returns></returns>
        public static DataTable GetPagedTable(DataTable dt, int PageIndex, int PageSize)
        {
            DataTable newdt = dt.Copy();
            newdt.Clear();

            int rowbegin = (PageIndex - 1) * PageSize;
            int rowend = PageIndex * PageSize;

            if (rowbegin >= dt.Rows.Count)
                return newdt;

            if (rowend > dt.Rows.Count)
                rowend = dt.Rows.Count;
            for (int i = rowbegin; i <= rowend - 1; i++)
            {
                DataRow newdr = newdt.NewRow();
                DataRow dr = dt.Rows[i];
                foreach (DataColumn column in dt.Columns)
                {
                    newdr[column.ColumnName] = dr[column.ColumnName];
                }
                newdt.Rows.Add(newdr);
            }
            return newdt;
        }
已标记关键词 清除标记
相关推荐
©️2020 CSDN 皮肤主题: 精致技术 设计师:CSDN官方博客 返回首页