ASP.net MVC分页实例

第一步:写Controllers里面的方法
        public ActionResult NewsList(int pgindex = 1)//默认索引为1
        {
            int counts = 0;//总的记录数
            int pageCount = 0;//总的页数

           //调用查询数据方法,下面会写出来
            ViewBag.newsList = new MvcHtmlString(NewsListMore("主题,大类,小类", "信息化,新闻", "", 1, pgindex, 20, "NewsList", out pageCount, out  counts, 14));

            //分页方法,直接在Views中生成分页标签,,,,,,,
            ViewBag.pagehtml = GetPageNum("/News/NewsList/", pgindex, 20, counts);
            return View();
        }

 

   #region 更多显示+NewsListMore()
        /// <summary>
        /// JoinComListPage
        /// </summary>
        /// <param name="getFiles">查询字段</param>
        /// <param name="SqlWhere">条件</param>
        /// <param name="orderFile"></param>
        /// <param name="ordefType">排列形式</param>
        /// <param name="pageIndex">索引</param>
        /// <param name="pageSize">每页条数</param>
        /// <param name="CssClass">Class</param>
        /// <param name="pageCount">页数</param>
        /// <param name="counts">总记录数</param>
        /// <returns></returns>
        public string NewsListMore(string getFiles, string SqlWhere, string orderFile, int ordefType, int pageIndex, int pageSize, string CssClass, out int pageCount, out int counts, int WordNum)
        {
            string html = "";
            pageCount = 0; counts = 0;
            classDBOP db = new classDBOP();
            string strWhere = "and 1=1 ";
            string files = "ID";//getFiles 赋值
            if (getFiles.Trim().Length == 0)
            {
                return "";
            }
            else
            {
                if (getFiles.IndexOf("主题") > -1)
                    files += ",Title";
                if (getFiles.IndexOf("大类") > -1)
                    files += ",BigClass";
                if (getFiles.IndexOf("小类") > -1)
                    files += ",SmallClass";
            }
            if (SqlWhere.Trim().Length == 0)
            {
                return "";
            }
            else  
            {
                if (SqlWhere.IndexOf("信息化") > -1)
                    strWhere += "and BigClass='信息化'";
                if (SqlWhere.IndexOf("新闻") > -1)
                    strWhere += "and SmallClass='新闻'";
            }
            switch (orderFile)
            {
                case "名称":
                    orderFile = "Title";
                    break;
                default:
                    orderFile = "id";
                    break;
            }
            if (orderFile.Trim().Length == 0)
            {
                orderFile = "id";
            }
            if (CssClass.Trim().Length == 0)
            {
                CssClass = "NewsList";
            }

        /// <summary>
        /// 查询,(直接返回dt)重载
        /// </summary>
        /// <param name="TableName">表名</param>
        /// <param name="SearchSQL">查询sql</param>
        /// <param name="GetFilds">要提取的字段</param>
        /// <param name="MainKey">主键字段</param>
        /// /// <param name="OrderFild">排序字段</param>
        /// <param name="OrderType">排序方式,0,1</param>
        /// <param name="SearchPageSize">条数</param>
        /// <param name="SearchPageIndex">页数</param>
        /// <param name="Dist">distinct:0/1</param>
        /// <param name="Counts">总记录数</param>
        /// <param name="PageCount">总页数</param>
        /// <param name="SafetySure">安全级别:1-字符+攻击,2-字符(公开新增加信息到库时必用),3-攻击(查询,新加都可能用到,前台查询时必用),4-无</param>
        /// <returns>DT</returns>
       /// public DataTable Search_DT(string TableName, string SearchSQL, string GetFilds, string OrderFild, string PrimaryID, int OrderType,

///int SearchPageSize, int SearchPageIndex, string Dist, out int PageCount, out int Counts, int SafetySure)
     ///   {
            DataTable dt = db.Search_DT("CTUInfo", strWhere, files, orderFile, "ID", ordefType, pageSize, pageIndex, "", out pageCount, out counts, 4);
            string strTitle = "";
            if (dt != null && dt.Rows.Count > 0)
            {
                html += "<div style='color:#293955;text-align:center;'>";
                html += "<table style='text-align:center;width:610px;border: #ccc 1px solid; line-height: 25px;' >";
                html += "<tr  class='backgroundimage'><td  width='250px' class=\"" + CssClass + "_head_name\"><strong>主题</strong></td>";
                html += "<td  float='left'  class=\"" + CssClass + "_head_sv\"><strong>大类</strong></td>";
                html += "<td  float='left'  width='130px'  class=\"" + CssClass + "_head_time\"><strong>小类</strong></td></tr>";
                for (int i = 0; i < dt.Rows.Count; i++)
                {
                    strTitle = dt.Rows[i]["Title"].ToString();
                    if (WordNum > 0 && strTitle.Length > WordNum)
                    {
                        strTitle = strTitle.Substring(0, WordNum);
                    }
                    html += "<tr  style='clear: both;border-bottom: 1px dashed #ccc; ' ><td style='clear: both;border-bottom: 1px dashed #ccc;text-align: left;padding-left: 20px' class=\"" + CssClass + "_name\"><a style='color:#000099;'  href='#'>" + strTitle + "</a></td>";
                    html += "<td  style='clear: both;border-bottom: 1px dashed #ccc;'  float='left'  class=\"" + CssClass + "_sv\">" + dt.Rows[i]["BigClass"] + "</td>";
                    html += "<td  style='clear: both;border-bottom: 1px dashed #ccc;'  float='left'  class=\"" + CssClass + "_time\">" + dt.Rows[i]["SmallClass"] + "</td></tr>";
                }
                html += "</table>";
                html += "</div>";
            }
            return html;
        }
        #endregion

 

       #region 分页
        public static string GetPageNum(string url, int page, int pagesize, int total)
        {
            int allpage = 0;
            int next = 0;
            int pre = 0;
            int startcount = 0;
            int endcount = 0;
            string pagestr = "";
            /* int total = 100;*/
            if (page < 1) { page = 1; }
            /*计算总页数*/
            if (pagesize != 0)
            {
                allpage = (total / pagesize);
                allpage = ((total % pagesize) != 0 ? allpage + 1 : allpage);
                allpage = (allpage == 0 ? 1 : allpage);
            }
            if (page > allpage)
            {
                page = allpage;
            }
            next = page + 1;
            pre = page - 1;
            startcount = (page + 5) > allpage ? allpage - 9 : page - 5;/*中间页起始序号*/
            /*中间页终止序号*/
            endcount = page < 5 ? 10 : page + 4;

            if (startcount < 1)
            {
                startcount = 1;
            } /*为了避免输出的时候产生负数,设置如果小于1就从序号1开始*/
            if (allpage < endcount)
            {
                endcount = allpage;
            } /*页码+5的可能性就会产生最终输出序号大于总页码,那么就要将其控制在页码数之内*/
            pagestr += "<span>共" + allpage + "页," + total + "条</span>";
            pagestr += page > 1 ? "<a href=\"" + url + "1\">首页</a><a href=\"" + url + "" + pre + "\">上一页</a>" : "";
            /*中间页处理,这个增加时间复杂度,减小空间复杂度 #ff0000:red*/
            for (int i = startcount; i <= endcount; i++)
            {
                pagestr += page == i ? "<font color=\"#000\">" + i + "</font>" : "<a href=\"" + url + "" + i + "\">" + i + "</a>";
            }
            pagestr += page != allpage ? "<a href=\"" + url + "" + next + "\">下一页</a><a href=\"" + url + "" + allpage + "\">末页</a>" : "";
            pagestr += "<input id='PagerInput' type='text' width='40px'/>";

            pagestr += "<a href='#' οnclick='LinkAction()'>跳转</a>";//InfoListPost\

            return pagestr;
        }
        #endregion

 

 

第二步:View页面

 <script type="text/javascript">
     function LinkAction() {
         var num = $("#PagerInput").val();
         window.location.href = '/News/NewsList/' + num;
     }
 </script>

<style type="text/css">
        body
        {
            font-size: 12px;
        }
        .pager
        {
            margin-bottom: 0px;
        }
        .pager a:hover
        {
            background: #99BBE8;
            color:red;
        }
        .pager span
        {
            color:green;
            font-size:12px;
        }
        .pager a
        {
            border:1px solid #5FA623;
            margin:0px 2px;
            background:#fff;
            padding:2px 5px;
            text-decoration: none;
            color:green;
            font-size:12px;
        }
    </style>

 

<div id="infolist" class="infolist">
    @ViewBag.newsList
    @{MvcHtmlString pagehtml = new MvcHtmlString(@ViewBag.pagehtml); }
 <div class="pager">@pagehtml</div>
</div>

 

 

第三部:数据库分页存储过程编写,这个在我的SQL中

  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值