zoukankan      html  css  js  c++  java
  • 分页

    PageInfo.cs
    using System;
    using System.Collections.Generic;
    using System.Linq;
    using System.Web;
    
    namespace WebPageNew.HtmlPage
    {
        public class PageInfo
        {
            /// <summary>
            /// 数据库中信息总条数
            /// </summary>
            public int TotalItems { get; set; }
    
            /// <summary>
            /// 每页条数
            /// </summary>
            public int ItemsPerPage { get; set; }
            /// <summary>
            /// 当前页
            /// </summary>
            public int CurrentPage { get; set; }
    
            /// <summary>
            /// 总页数
            /// </summary>
            public int TotalPages
            {
                get { return (int)Math.Ceiling((decimal)TotalItems / ItemsPerPage); }
            }
        }
    }
    Paging.cs
    using System;
    using System.Collections.Generic;
    using System.Linq;
    using System.Web;
    using System.Text;
    
    namespace WebPageNew.HtmlPage
    {
        public class Paging
        {
            /// <summary>
            /// 最新完整分页。2013年5月5日更新,在学习老赵的UserControl中应用到。
            /// </summary>
            /// <param name="pageInfo"></param>
            /// <param name="PageUrl">当前页面的地址</param>
            /// <returns></returns>
            public string MyPage(PageInfo pageInfo, string PageUrl)
            {
    
                int Prev = pageInfo.CurrentPage - 1;
                int Next = pageInfo.CurrentPage + 1;
                StringBuilder result = new StringBuilder();
                //首页
                string strFirst = string.Empty;
                strFirst = "<a href='" + PageUrl + "?PageNumber=1 '>Frist</a>";
                result.Append(strFirst);
                //上一页
                string strPrev = string.Empty;
                if (Prev < 1)
                    Prev = 1;
                strPrev = "<a href='" + PageUrl + "?PageNumber=" + Prev.ToString() + " '>Prev</a>";
                result.Append(strPrev);
    
    
                //页码
                int PageNumSize = 11;
                int StartPage = 1;
                string strPage = string.Empty;
                if (pageInfo.CurrentPage > 5)//如果当前页面大于5
                {
                    StartPage = pageInfo.CurrentPage - 5;//页码开始的序号为:当前页 - 5
                    PageNumSize = pageInfo.CurrentPage + 5;//页面的最后为:当前页 + 5
                }
                if (pageInfo.TotalPages <= 11)//如果总页数小于11,那么设置显示的总页数为:小于11的那个页码
                    PageNumSize = pageInfo.TotalPages;
    
                if (PageNumSize > pageInfo.TotalPages)
                    PageNumSize = pageInfo.TotalPages;
                //----
                int startPage = 1;
                int lastPage = 11;
                if (pageInfo.TotalPages <= 11)
                {
                    startPage = 1;
                    lastPage = pageInfo.TotalPages;
                }
                else
                {
                    if (pageInfo.CurrentPage > 5)
                    {
                        startPage = pageInfo.CurrentPage - 5;
                        lastPage = pageInfo.CurrentPage + 5;
                        if (lastPage > pageInfo.TotalPages)
                        {
                            startPage = pageInfo.TotalPages - 10;
                            lastPage = pageInfo.TotalPages;
                        }
                    }
                }
                //页号----------------
                for (int i = startPage; i <= lastPage; i++)
                {
                    if (i == pageInfo.CurrentPage)
                        strPage = "<a class=\"selected\" href=\"" + PageUrl + "\">" + i.ToString() + "</a>";
                    else
                        strPage = "<a href='" + PageUrl + "?PageNumber=" + i.ToString() + "'>" + i.ToString() + "</a>";
                    result.Append(strPage);
                }
    
    
    
    
    
    
    
    
                //下一页
                string strNext = string.Empty;
                if (Next > pageInfo.TotalPages)
                    Next = pageInfo.TotalPages;
                strNext = "<a href='" + PageUrl + "?PageNumber=" + Next.ToString() + " '>Next</a>";
                result.Append(strNext);
                //末页
                string strLast = string.Empty;
                strLast = "<a href='" + PageUrl + "?PageNumber=" + pageInfo.TotalPages + " '>Last</a>";
                result.Append(strLast);
    
                return result.ToString();
            }
        }
    }
    Default.aspx
    <%@ Page Language="C#" AutoEventWireup="true" CodeBehind="Default.aspx.cs" Inherits="WebPageNew.Default" %>
    
    <!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">
            #head .title{ height:80px; background-color:#444444;}
            #Pageing A{ background-color:#008844;
                        border:1px solid White;
                        text-decoration:none; color:White;
                        padding: .1em .6em .2em .6em;}
            #Pageing A.selected{ background-color:#AA7700;}
            body
    {
        font-size: .85em;
        font-family: "Trebuchet MS", Verdana, Helvetica, Sans-Serif;
        color: #232323;
        background-color: #fff;
    }
        </style>
    </head>
    <body>
        <form id="form1" runat="server">
        <div>
           <asp:DataList ID="DataList1" runat="server">
                
            </asp:DataList>
         <div id="Pageing" style="padding-bottom:10px;">
            <%= str %>
        </div>
        </div>
        </form>
    </body>
    </html>
    Default.aspx.cs
    using System;
    using System.Collections.Generic;
    using System.Linq;
    using System.Web;
    using System.Web.UI;
    using System.Web.UI.WebControls;
    using WebPageNew.HtmlPage;
    
    namespace WebPageNew
    {
        public partial class Default : System.Web.UI.Page
        {
            protected string str = "";
            protected void Page_Load(object sender, EventArgs e)
            {
                //设置page信息
                string strPageNum = Request.QueryString["PageNumber"];
                if (string.IsNullOrEmpty(strPageNum))
                    strPageNum = "1";
    
                #region 设置分页信息
                //数据库中获取消息总条数TotalItems
                //设置每页条数
                //设置当前页
                PageInfo pageinfo = new PageInfo
                {
                    CurrentPage = Convert.ToInt32(strPageNum),
                    ItemsPerPage = 11,
                    TotalItems = 200
                };
                #endregion
    
                #region 数据绑定
                //根据pageinfo请求数据库数据,绑定到datalist
                //当前页 pageinfo.CurrentPage
                //每页数据条数 pageinfo.ItemPerPage
                //计算出取 n -- n+10条
                #endregion
    
                #region 输出page
                Paging p = new Paging();
                str = p.MyPage(pageinfo, "http://localhost:5867/Default.aspx");
                #endregion
            }
        }
    }

    实例下载

  • 相关阅读:
    【网络安全设备系列】2、IDS(入侵检测系统)
    【网络安全设备系列】4、漏洞扫描设备
    【网络安全设备系列】3、IPS(入侵防御系统)
    【网络安全设备系列】1、防火墙
    [操作系统作业]os
    [web知识]网页当中使用base64表示png图片
    [struts]异常Caught Exception while registering Interceptor class org.crazyit.auction.action.interceptor.AuthorityInterceptor interceptor
    [struts]使用struts验证框架验证表单输入如何配置Action
    [操作系统作业]os实验三:进程的管道通信
    [spring]异常 Illegal UTF8 string in constant pool in class file com/mchange/lang/PotentiallySecondaryException
  • 原文地址:https://www.cnblogs.com/wupeiqi/p/3062036.html
Copyright © 2011-2022 走看看