静态页 分页导航条生成类

在生成静态列表页时,肯定要生成分页导航条,下面这个类就封装了两个导航条生成函数。

建议编译成dll组件后,在项目中引用

using System;
using System.Collections.Generic;
using System.Text;


namespace Pagination
{
    /// <summary>
    /// 完成分页导航的生成
    /// </summary>
    public class PageNavigate
    {
        private PageNavigate() { }
        /// <summary>
        /// 生成分页导航条
        /// </summary>
        /// <param name="totalRecord">总记录数</param>
        /// <param name="pageSize">页面记录数</param>
        /// <param name="DistLen">数字长度</param>
        /// <param name="page">当前页数</param>
        /// <param name="QueryParametet">URL参数</param>
        /// <param name="scriptName">页名前缀</param>
        /// <returns></returns>
         public static string GetNavigate(int totalRecord,int pageSize,int DistLen,int page,string QueryParameter,string scriptName,string extName)
         {

             int pageNum; //页面数量
             string HTML="";
             if ((totalRecord % pageSize) != 0)
                 pageNum = (totalRecord / pageSize) + 1;
             else
                 pageNum = totalRecord / pageSize;

             if (pageNum < 1)
                 return HTML;
             //统计信息
             HTML += "显示" + ((page - 1) * pageSize + 1) + "━"; 
             if (page >= pageNum)
                 HTML += totalRecord + "条";
             else
                 HTML += page * pageSize + "条";
           
             HTML += "共<B>" + totalRecord + "</B>条 " + pageSize + "条/页";
            
             //首页,上一页
             if (page > 1)
             {
                 HTML += "<a href=/"" + scriptName + "1" + extName + "?" + QueryParameter + "/"  > 首  页 </a>";

                 HTML += "<a href=/"" + scriptName + (page - 1) + extName + "?" + QueryParameter + "/" > 上一页 </a>";
             }

             //数字序列
             int page_t;
             if (page < DistLen * 2)
             {
                 if (pageNum > DistLen * 2)

                     page_t = DistLen * 2;
                 else
                     page_t = pageNum;

                 for (int i = 1; i <= page_t; i++)
                 {
                     if (i == page)
                         HTML += "<B>&nbsp;" + page + "</B>";
                     else
                         HTML += "<a href=/""+ scriptName + i + extName+ "?" +QueryParameter+ "/">&nbsp;" + i + "</a>";

                 }
             }
             else
             {
                 if (pageNum > (page + DistLen))
                     page_t = page + DistLen;
                 else
                     page_t = pageNum;

                 for (int i = (page - DistLen); i <= page_t; i++)
                 {
                     if (i == page)
                         HTML += "<B>&nbsp;" + page + "</B>";
                     else
                         HTML += "<a href=/"" + scriptName + i + extName + "?" + QueryParameter + "/" >&nbsp;" + i + "</a>";

                 }

             }
             //下一页,尾页
             if (page < pageNum)
             {
                 HTML += "<a href=/"" + scriptName + (page + 1) + extName + "?" + QueryParameter + "/" > 下一页 </a>";
                 HTML += "<a href=/"" + scriptName + pageNum + extName + "?" + QueryParameter + "/" > 尾页 </a>";
             }

             return HTML;
         }
        /// <summary>
        /// 带下拉框的分页
        /// </summary>
        /// <param name="pageCount">总页数</param>
        /// <param name="page">当前页</param>
        /// <param name="pageSize">页面大小</param>
        /// <param name="QueryParameter">页面参数</param>
        /// <param name="scriptName">页面前缀</param>
        /// <returns></returns>
        public static string GetNavigate(int pageCount, int page, int pageSize, string QueryParameter, string scriptName,string extName)
        {
            string HTML = "";
            if (pageCount < 1) return HTML;
            //首页,上一页
            if (page > 1)
            {
                HTML += "<a href=/"" + scriptName + "1" + extName + "?" + QueryParameter + "/"> 首页 </a>";
                HTML += "<a herf=/"" + scriptName + (page - 1) + extName + "?" + QueryParameter + "/"> 上一页 </a>";
            }
            else
            {
                HTML += " 首页  上一页 ";
            }

            //下一页,尾页
            if (page < pageCount)
            {
                HTML +="<a href=/"" + scriptName + (page+1)+ extName +"?" + QueryParameter + "/"> 下一页 </a>";
                HTML += "<a href=/"" + scriptName + (pageCount) + extName +"?"  + QueryParameter + "/"> 尾页 </a>";
            }
            else
            {

                HTML += " 下一页  尾页 ";
            }
            HTML += " <select name=/"page___/" id=/"page___/" onChange=/" GoToSelectedPage();/">" + Environment.NewLine; ;
            HTML += " <option> 选择 </option>" +Environment.NewLine;
            for (int i = 1; i <= pageCount; i++)
            {
                if (i == page)
                    HTML += "<option value=/"" + scriptName +  i + extName + "?" + QueryParameter+ "/" selected>" + i + "</option>" + Environment.NewLine;
                else
                    HTML += "<option value=/"" + scriptName + i + extName + "?" + QueryParameter + "/">" + i + "</option>" + Environment.NewLine;
            }
            HTML += "</select>" + Environment.NewLine;
            HTML += "<script language=/"JScript/">" + Environment.NewLine;
            HTML += "<!--" + Environment.NewLine;
            HTML += " function GoToSelectedPage(){ " + Environment.NewLine;
            HTML += " var page___=document.getElementById(/"page___/");" + Environment.NewLine;
            HTML += " var index=page___.selectedIndex;" + Environment.NewLine;
            HTML += " if(index==0) return;" + Environment.NewLine;
            HTML += " window.location=page___.value;" +Environment.NewLine;
            HTML += " }" + Environment.NewLine;
            HTML += " //-->" + Environment.NewLine;
            HTML += " </script>" + Environment.NewLine;
            return HTML;

        }
        /// <summary>
        /// 计算页面数量
        /// </summary>
        /// <param name="totalRecord">总记录数</param>
        /// <param name="pageSize">页面大小</param>
        /// <returns>页面数量</returns>
        public static int GetPageCount(int totalRecord, int pageSize)
        {
            if (totalRecord % pageSize == 0)
                return totalRecord / pageSize;
            else
                return (totalRecord / pageSize) + 1;

        }
        /// <summary>
        /// 返回合成的文件名
        /// </summary>
        /// <param name="scritpName">页面前缀</param>
        /// <param name="page">当前页</param>
        /// <param name="extName">扩展名</param>
        /// <returns></returns>
        public static string GetShortFileName(string scritpName,int page,string extName)
        {
            return scritpName + page + extName;
        }

     }

}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值