注册 登录  
 加关注
查看详情
   显示下一条  |  关闭
温馨提示!由于新浪微博认证机制调整,您的新浪微博帐号绑定已过期,请重新绑定!立即重新绑定新浪微博》  |  关闭

一车,一房,一个温暖家

10万左右的车,80平方的房子,这是我奋斗的暖暖的家

 
 
 

日志

 
 

引用 分页类   

2015-05-12 21:15:16|  分类: 默认分类 |  标签: |举报 |字号 订阅

  下载LOFTER 我的照片书  |
本文转载自那么骄傲《分页类》

using System;
using System.Collections.Generic;
using System.Text;
using System.Data;
using System.Data.SqlClient;

namespace SiteClass
{
    public class AspNetPage
    {
        //数据库连接对象      
        private SqlConnection connPage = new SqlConnection(SQLDatabase.ConnectionString);

        public int DataCounts;      //总记录数
        public int PageCount;      //总页数
        public int inPage;         //当前页数
        public int inCount;        //提取记录数
        public string inTable;     //查询的表或者视图
        public string inRow;       //需要排列的字段
        public string inSql;       //sql语句
        public string inFieldsName; //查询的段
        public string inGroupbyStr; //分组
        public string inOrderByStr; //排序

        public AspNetPage()
        { }

        /// <summary>
        /// 访问数据库
        /// </summary>
        /// <param name="dbname">数据库名称</param>
        public AspNetPage(string dbname)
        {
            if (!string.IsNullOrEmpty(dbname))
            {
                if (dbname == "NewDingFangNet")  //默认主数据库
                    connPage = new SqlConnection(SQLDatabase.ConnNewDingFangNet);
                else
                    connPage = new SqlConnection(SQLDatabase.ConnectionString);
            }
        }

        #region 后台自定义分页
        /// <summary>
        /// 后台自定义分页
        /// </summary>
        /// <param name="inUrl">当前页url</param>
        /// <param name="inNowpage">当前页码</param>
        /// <returns></returns>
        public string GetNewPageList(string inUrl, int inNowpage)
        {
            //inNowpage 当前页
            System.Text.StringBuilder sb = new System.Text.StringBuilder();
            int nPageStart = 0;
            int nPageEnd = 0;
            int i = 0;
            //#################分页规则begin
            if (inPage + 7 > PageCount)
            {
                if (inPage - 7 < 1)
                {
                    nPageStart = 1;
                }
                else
                {
                    nPageStart = PageCount - 9;
                }
                nPageEnd = PageCount;
            }
            else
            {
                if (inPage - 2 < 1)
                {
                    nPageStart = 1;
                    nPageEnd = inPage + 8;
                }
                else
                {
                    nPageStart = inPage - 2;
                    nPageEnd = inPage + 6;
                }
            }
            //#################分页规则end
            sb.Append("<span>共" + DataCounts + "条</span>");
            if (Convert.ToInt32(inNowpage) > 3)
            {
                sb.Append(" <a href=" + Convert.ToString(inUrl) + ">1...</a>");
            }
            if (!string.IsNullOrEmpty(inNowpage.ToString()) & Convert.ToInt32(inNowpage) > 1)
            {
                sb.Append(" <a href=" + Convert.ToString(inUrl) + "&page=" + Convert.ToString(Convert.ToInt32(inNowpage) - 1) + ">上一页</a>");
            }
            for (i = nPageStart; i <= nPageEnd; i++)
            {
                if (i == inPage)
                {
                    sb.Append(" <span class=\"cpb\">" + i.ToString() + "</span>");
                }
                else
                {
                    sb.Append(" <a href=\"" + Convert.ToString(inUrl) + "&page=" + i.ToString() + "\" >" + i.ToString() + "</a>");
                }
            }
            if (!string.IsNullOrEmpty(inNowpage.ToString()))
            {
                if (inNowpage < nPageEnd)
                {
                    sb.Append(" <a href=\"" + Convert.ToString(inUrl) + "&page=" + Convert.ToString(Convert.ToInt32(inNowpage) + 1) + "\" >下一页</a>");
                }
            }
            else
            {
                if (nPageEnd > 1)
                {
                    sb.Append(" <a href=\"" + Convert.ToString(inUrl) + "&page=" + Convert.ToString(2) + "\">下一页</a>");
                }
            }
            if (nPageEnd < PageCount)
            {
                sb.Append(" <a href=" + Convert.ToString(inUrl) + "&page=" + PageCount + " >" + "..." + PageCount + "</a>");
            }
            //#################
            return sb.ToString();
        }
        #endregion

        #region 后台自定义分页 - 可跳转的页码
        /// <summary>
        /// 后台自定义分页  -- 可跳转的页码
        /// </summary>
        /// <param name="inUrl">当前页url</param>
        /// <param name="inNowpage">当前页码</param>
        /// <returns></returns>
        public string GetNewPage_TurnTo(string inUrl, int inNowpage)
        {
            //inNowpage 当前页
            System.Text.StringBuilder sb = new System.Text.StringBuilder();
            int nPageStart = 0;
            int nPageEnd = 0;
            int i = 0;
            //#################分页规则begin
            if (inPage + 7 > PageCount)
            {
                if (inPage - 7 < 1)
                {
                    nPageStart = 1;
                }
                else
                {
                    nPageStart = PageCount - 9;
                }
                nPageEnd = PageCount;
            }
            else
            {
                if (inPage - 2 < 1)
                {
                    nPageStart = 1;
                    nPageEnd = inPage + 8;
                }
                else
                {
                    nPageStart = inPage - 2;
                    nPageEnd = inPage + 6;
                }
            }
            //#################分页规则end
            sb.Append("<span>共" + DataCounts + "条</span>");
            if (Convert.ToInt32(inNowpage) > 3)
            {
                sb.Append(" <a href=" + Convert.ToString(inUrl) + ">1...</a>");
            }
            if (!string.IsNullOrEmpty(inNowpage.ToString()) & Convert.ToInt32(inNowpage) > 1)
            {
                sb.Append(" <a href=" + Convert.ToString(inUrl) + "&page=" + Convert.ToString(Convert.ToInt32(inNowpage) - 1) + ">上一页</a>");
            }
            for (i = nPageStart; i <= nPageEnd; i++)
            {
                if (i == inPage)
                {
                    sb.Append(" <a href=\"#\" class=\"pgon\">" + i.ToString() + "</a>");
                }
                else
                {
                    sb.Append(" <a href=\"" + Convert.ToString(inUrl) + "&page=" + i.ToString() + "\" >" + i.ToString() + "</a>");
                }
            }
            if (!string.IsNullOrEmpty(inNowpage.ToString()))
            {
                if (inNowpage < nPageEnd)
                {
                    sb.Append(" <a href=\"" + Convert.ToString(inUrl) + "&page=" + Convert.ToString(Convert.ToInt32(inNowpage) + 1) + "\" >下一页</a>");
                }
            }
            else
            {
                if (nPageEnd > 1)
                {
                    sb.Append(" <a href=\"" + Convert.ToString(inUrl) + "&page=" + Convert.ToString(2) + "\">下一页</a>");
                }
            }
            if (nPageEnd < PageCount)
            {
                sb.Append(" <a href=" + Convert.ToString(inUrl) + "&page=" + PageCount + " >" + "..." + PageCount + "</a>");
            }
            //#################

            //sb.Append("<span>共" + PageCount + "页</span>");
            if (PageCount > 1)
            {
                string pageurl = "";
                if (!inUrl.Contains("page="))
                {
                    if (inUrl.Contains("?"))
                    {
                        pageurl = inUrl;
                    }
                    else
                    {
                        pageurl = inUrl + "?wq=%E8%BF%";
                    }
                }
                else
                {
                    pageurl = inUrl.Substring(0, inUrl.IndexOf("page=") - 1);
                }
                sb.Append("  转到<select name='select' id='pa' onChange='window.location.href=\"" + pageurl + "&page=\"+document.all.pa.value'>");
                string Sselected = "";
                for (int u = 1; u <= PageCount; u++)
                {
                    Sselected = "";
                    if (u == inNowpage)
                        Sselected = "selected";

                    sb.Append("<option value='" + u.ToString() + "'" +Sselected+ ">" + u.ToString() + "</option>");
                }
                sb.Append("</select>页");
            }

            return sb.ToString();
        }
        #endregion

        #region 多列排序的SQL存储过程分页
        //多列排序的SQL存储过程分页
        public DataTable GetPage_DataTable()
        {
            SqlDataAdapter MyDataAdapter = default(SqlDataAdapter);
            DataSet MyDataSet = null;
            //创建DataSet对象
            DataTable MyDataTable = null;
            //创建MyDataTable对象
            SqlConnection MyConnection = connPage;
            MyConnection.Open();
            SqlCommand MyCommand = new SqlCommand();
            MyCommand.CommandType = CommandType.StoredProcedure;
            MyCommand.CommandText = "Pro_AspNetPage";
            MyCommand.Connection = MyConnection;
            MyDataAdapter = new SqlDataAdapter(MyCommand);
            MyCommand.Parameters.Add(new SqlParameter("Return_Value", SqlDbType.Int));
            MyCommand.Parameters.Add(new SqlParameter("@CountRows", SqlDbType.Int));
            MyCommand.Parameters["Return_Value"].Direction = ParameterDirection.ReturnValue;
            MyCommand.Parameters["@CountRows"].Direction = ParameterDirection.Output;
            MyCommand.Parameters.Add(new SqlParameter("@FromStr", SqlDbType.VarChar, 1000)).Value = inTable;
            MyCommand.Parameters.Add(new SqlParameter("@SelectStr", SqlDbType.VarChar, 2000)).Value = inFieldsName;
            MyCommand.Parameters.Add(new SqlParameter("@FdName", SqlDbType.VarChar, 100)).Value = inRow;
            MyCommand.Parameters.Add(new SqlParameter("@PageSize", SqlDbType.Int)).Value = inCount;
            MyCommand.Parameters.Add(new SqlParameter("@PageCurrent", SqlDbType.Int)).Value = inPage;
            MyCommand.Parameters.Add(new SqlParameter("@OrderByStr", SqlDbType.VarChar, 1000)).Value = inOrderByStr;
            MyCommand.Parameters.Add(new SqlParameter("@WhereStr ", SqlDbType.VarChar, 2000)).Value = inSql;
            MyDataSet = new DataSet();
            try
            {
                MyDataAdapter.Fill(MyDataSet);
                MyDataTable = MyDataSet.Tables[0];
                if (MyCommand.Parameters["Return_Value"].Value.ToString() == "0")
                {
                    DataCounts = Convert.ToInt32 (MyCommand.Parameters["@CountRows"].Value.ToString());
                }
                PageCount = Convert.ToInt32 (DataCounts / inCount);
                //计算总页数
                if (DataCounts % inCount > 0)
                {
                    PageCount += 1;
                }
                return MyDataTable;

            }
            catch (Exception ex)
            {
                //写入系统日志
                //sc_Common.blnInsertLogForFiles("得到AspNetPage分页查询错误:" + ex.Message, "AspNetPage分页查询", System.Web.HttpContext.Current.Request.UserHostAddress);
                AppLog.Write("得到AspNetPage分页查询错误--IP:" + System.Web.HttpContext.Current.Request.UserHostAddress,AppLog.LogMessageType.Error,ex,typeof(AspNetPage));
                return MyDataTable;
            }
            finally
            {
                MyConnection.Close();
                MyCommand = null;
                MyDataAdapter = null;
                MyDataSet = null;
            }
        }
         #endregion
       
        #region 自定义分页

        /// <summary>
        /// 自定义分页--不用过URL
        /// </summary>
        /// <param name="strUrl"></param>
        /// <param name="iNewpage"></param>
        /// <returns></returns>
        public string GetNewPageList(int iNewpage)
        {
            System.Text.StringBuilder str = new System.Text.StringBuilder();
           
            int nPageStart = 0;
            int nPageEnd = 0;
            int i = 0;
            //#################分页规则begin
            if (inPage + 7 > PageCount)
            {
                if (inPage - 7 < 1)
                {
                    nPageStart = 1;
                }
                else
                {
                    nPageStart = PageCount - 9;
                }
                nPageEnd = PageCount;
            }
            else
            {
                if (inPage - 2 < 1)
                {
                    nPageStart = 1;
                    nPageEnd = inPage + 8;
                }
                else
                {
                    nPageStart = inPage - 2;
                    nPageEnd = inPage + 6;
                }
            }
            //#################分页规则end
            str.Append("<span>共" + DataCounts + "条</span>");
            if (Convert.ToInt32(iNewpage) > 3)
            {
                str.Append(" <a onclick='Page(1)' href='javascript:Page(1);'>1...</a>");
            }
            if (!string.IsNullOrEmpty(iNewpage.ToString()) & Convert.ToInt32(iNewpage) > 1)
            {
                str.Append(" <a onclick='Page(" + Convert.ToString(Convert.ToInt32(iNewpage) - 1) + ")' href='javascript:Page(" + Convert.ToString(Convert.ToInt32(iNewpage) - 1) + ");'>上一页</a>");
            }
            for (i = nPageStart; i <= nPageEnd; i++)
            {
                if (i == inPage)
                {
                    str.Append(" <span class=\"cpb\">" + i.ToString() + "</span>");
                }
                else
                {
                    str.Append(" <a onclick='Page(" + i.ToString() + ")' href='javascript:Page(" + i.ToString() + ");'>" + i.ToString() + "</a>");
                }
            }
            if (!string.IsNullOrEmpty(iNewpage.ToString()))
            {
                if (iNewpage < nPageEnd)
                {
                    //str.Append(" <a href=\"" + Convert.ToString(strUrl) + "&page=" + Convert.ToString(Convert.ToInt32(iNewpage) + 1) + "\" >下一页</a>");
                    str.Append(" <a onclick='Page(" + Convert.ToString(Convert.ToInt32(iNewpage) + 1) + ")' href='javascript:Page(" + Convert.ToString(Convert.ToInt32(iNewpage) + 1) + ");'>下一页</a>");
                }
            }
            else
            {
                if (nPageEnd > 1)
                {
                    //str.Append(" <a href=\"" + Convert.ToString(strUrl) + "&page=" + Convert.ToString(2) + "\">下一页</a>");
                    str.Append(" <a onclick='Page(2)' href='javascript:Page(2);'>下一页</a>");
                }
            }
            if (nPageEnd < PageCount)
            {
                //str.Append(" <a href=" + Convert.ToString(strUrl) + "&page=" + PageCount + " >" + "..." + PageCount + "</a>");
                str.Append(" <a onclick='Page(" + PageCount + ")' href='javascript:Page(" + PageCount + ");'>...</a>");
            }
          
            return str.ToString();
        }

        #endregion
    }
}

  评论这张
 
阅读(0)| 评论(0)
推荐 转载

历史上的今天

在LOFTER的更多文章

评论

<#--最新日志,群博日志--> <#--推荐日志--> <#--引用记录--> <#--博主推荐--> <#--随机阅读--> <#--首页推荐--> <#--历史上的今天--> <#--被推荐日志--> <#--上一篇,下一篇--> <#-- 热度 --> <#-- 网易新闻广告 --> <#--右边模块结构--> <#--评论模块结构--> <#--引用模块结构--> <#--博主发起的投票-->
 
 
 
 
 
 
 
 
 
 
 
 
 
 

页脚

网易公司版权所有 ©1997-2018