c# 使用mvc 分页控件
/// <summary> /// 获得伪静态页码显示链接 /// </summary> /// <param name="curPage">当前页数</param> /// <param name="countPage">总页数</param> /// <param name="url">超级链接地址</param> /// <param name="extendPage">周边页码显示个数上限</param> /// <returns>页码html</returns> public static string GetStaticPageNumbers(int curPage, int countPage, string url, int extendPage) { int startPage = 1; int endPage = 1; string t1 = "<a title=\"首页\" href=\"" + string.Format(url, "1") + "\">首页</a><a title=\"上一页\" href=\"" + string.Format(url, (curPage - 1)) + "\">上一页</a>"; string t2 = "<a title=\"下一页\" href=\"" + string.Format(url, (curPage + 1)) + "\">下一页</a><a title=\"尾页\" href=\"" + string.Format(url, countPage) + "\">尾页</a>"; if (countPage < 1) countPage = 1; if (extendPage < 3) extendPage = 2; if (countPage > extendPage) { if (curPage - (extendPage / 2) > 0) { if (curPage + (extendPage / 2) < countPage) { startPage = curPage - (extendPage / 2); endPage = startPage + extendPage - 1; } else { endPage = countPage; startPage = endPage - extendPage + 1; t2 = "<a title=\"下一页\" class=\"disabled next\" href=\"javascript:;\">下一页</a><a class=\"disabled end\" title=\"尾页\" href=\"javascript:;\">尾页</a>"; } } else { endPage = extendPage; t1 = "<a title=\"首页\" class=\"disabled begin\" href=\"javascript:;\">首页</a><a class=\"disabled previou\" title=\"上一页\" href=\"javascript:;\">上一页</a>"; } } else { startPage = 1; endPage = countPage; t1 = "<a title=\"首页\" class=\"disabled begin\" href=\"javascript:;\">首页</a><a class=\"disabled previou\" title=\"上一页\" href=\"javascript:;\">上一页</a>"; t2 = "<a title=\"下一页\" class=\"disabled next\" href=\"javascript:;\">下一页</a><a class=\"disabled end\" title=\"尾页\" href=\"javascript:;\">尾页</a>"; } if (curPage == 1 && curPage != endPage) { t2 = "<a title=\"下一页\" class=\"next\" href=\"" + string.Format(url, (curPage + 1)) + "\">下一页</a><a class=\"end\" title=\"尾页\" href=\"" + string.Format(url, countPage) + "\">尾页</a>"; } else if (curPage < endPage && curPage != 1) { t2 = "<a title=\"下一页\" class=\"next\" href=\"" + string.Format(url, (curPage + 1)) + "\">下一页</a><a class=\"end\" title=\"尾页\" href=\"" + string.Format(url, countPage) + "\">尾页</a>"; } if (curPage > 1 && curPage != endPage) { t1 = "<a title=\"首页\" class=\"begin\" href=\"" + string.Format(url, "1") + "\">首页</a><a class=\"previou\" title=\"上一页\" href=\"" + string.Format(url, (curPage - 1)) + "\">上一页</a>"; } else if (curPage == endPage && curPage != 1) { t1 = "<a title=\"首页\" class=\"begin\" href=\"" + string.Format(url, "1") + "\">首页</a><a class=\"previou\" title=\"上一页\" href=\"" + string.Format(url, (curPage - 1)) + "\">上一页</a>"; } StringBuilder s = new StringBuilder(""); s.Append(t1); for (int i = startPage; i <= endPage; i++) { if (i == curPage) { s.Append("<a href=\"javascript:;\" class=\"active\" title=\"第 " + i + " 页\">"); s.Append(i); s.Append("</a>"); } else { s.Append("<a title=\"第 " + i + " 页\" href=\""); s.Append(string.Format(url, i)); s.Append("\">"); s.Append(i); s.Append("</a>"); } } s.Append(t2); return s.ToString(); } /// <summary> /// 获得AJAX页码显示链接 /// </summary> /// <param name="curPage">当前页数</param> /// <param name="countPage">总页数</param> /// <param name="url">超级链接地址</param> /// <param name="extendPage">周边页码显示个数上限</param> /// <returns>页码html</returns> public static string GetAjaxPageNumbers(int curPage, int countPage, string url, int extendPage) { int startPage = 1; int endPage = 1; string t1 = "<a title=\"首页\" href=\"javascript:;\" onclick=\"" + url.Replace("{0}", "1") + "\">首页</a><a title=\"上一页\" href=\"javascript:;\" onclick=\"" + url.Replace("{0}", (curPage - 1).ToString()) + "\">上一页</a>"; string t2 = "<a title=\"下一页\" href=\"javascript:;\" onclick=\"" + url.Replace("{0}", (curPage + 1).ToString()) + "\">下一页</a><a title=\"尾页\" href=\"javascript:;\" onclick=\"" + url.Replace("{0}", countPage.ToString()) + "\">尾页</a>"; string t3 = ""; string t4 = ""; if (countPage < 1) countPage = 1; if (extendPage < 3) extendPage = 2; if (countPage > extendPage) { if (curPage - (extendPage / 2) > 0) { if (curPage + (extendPage / 2) < countPage) { startPage = curPage - (extendPage / 2); endPage = startPage + extendPage - 1; } else { endPage = countPage; startPage = endPage - extendPage + 1; if (curPage >= countPage) t2 = "<a class=\"disabled\" title=\"下一页\" href=\"javascript:;\">下一页</a><a class=\"disabled\" title=\"尾页\" href=\"javascript:;\">尾页</a>"; } } else { endPage = extendPage; if (curPage <= 1 || countPage == 1) t1 = "<a class=\"disabled\" title=\"首页\" href=\"javascript:;" + "\">首页</a><a class=\"disabled\" title=\"上一页\" href=\"javascript:;\">上一页</a>"; } } else { startPage = 1; endPage = countPage; if (curPage <= 1 || countPage == 1) t1 = "<a class=\"disabled\" title=\"首页\" href=\"javascript:;" + "\">首页</a><a class=\"disabled\" title=\"上一页\" href=\"javascript:;\">上一页</a>"; if (curPage >= countPage) t2 = "<a class=\"disabled\" title=\"下一页\" href=\"javascript:;\">下一页</a><a class=\"disabled\" title=\"尾页\" href=\"javascript:;\">尾页</a>"; } StringBuilder s = new StringBuilder(""); s.Append(t1); if (curPage > (extendPage / 2) + 1) t3 = "<a title=\"上翻\" href=\"javascript:;\" onclick=\"" + url.Replace("{0}", startPage.ToString()) + "\">...</a>"; s.Append(t3); for (int i = startPage; i <= endPage; i++) { if (i == curPage) { s.Append("<a class=\"active\" href=\"javascript:;\" title=\"第 " + i + " 页\"> "); s.Append(i); s.Append("</a>"); } else { s.Append("<a title=\"第 " + i + " 页\" href=\"javascript:;\""); s.Append(" onclick=\""); s.Append(url.Replace("{0}", i.ToString())); s.Append("\">"); s.Append(i); s.Append("</a>"); } } if (curPage + (extendPage / 2) < countPage) t4 = "<a title=\"下翻\" href=\"javascript:;\" onclick=\"" + url.Replace("{0}", endPage.ToString()) + "\">...</a>"; s.Append(t4); s.Append(t2); return s.ToString(); } /// <summary> /// 获得AJAX页码显示链接 /// </summary> /// <param name="curPage">当前页数</param> /// <param name="countPage">总页数</param> /// <param name="url">超级链接地址</param> /// <param name="extendPage">周边页码显示个数上限</param> /// <returns>页码html</returns> public static string GetAjaxPageNumbers(int curPage, int countPage, string url, int extendPage, bool isInstitutional = false) { int startPage = 1; int endPage = 1; //string t1 = "<a title=\"首页\" href=\"javascript:;\" onclick=\"" + url.Replace("{0}", "1") + "\">首页</a><a title=\"上一页\" href=\"javascript:;\" onclick=\"" + url.Replace("{0}", (curPage - 1).ToString()) + "\">上一页</a>"; string t1 = "<a href=\"javascript:;\" class=\"layui-laypage-prev\" data-page=\"" + (curPage - 1) + "\" onclick=\"" + url.Replace("{0}", (curPage - 1).ToString()) + "\">上一页</a>"; string t2 = "<a href=\"javascript:;\" class=\"layui-laypage-next\" data-page=\""+ (curPage + 1) + "\" onclick=\"" + url.Replace("{0}", (curPage + 1).ToString()) + "\">下一页</a>"; //string t2 = "<a title=\"下一页\" href=\"javascript:;\" onclick=\"" + url.Replace("{0}", (curPage + 1).ToString()) + "\">下一页</a><a title=\"尾页\" href=\"javascript:;\" onclick=\"" + url.Replace("{0}", countPage.ToString()) + "\">尾页</a>"; string t3 = ""; string t4 = ""; if (countPage < 1) countPage = 1; if (extendPage < 3) extendPage = 2; if (countPage > extendPage) { if (curPage - (extendPage / 2) > 0) { if (curPage + (extendPage / 2) < countPage) { startPage = curPage - (extendPage / 2); endPage = startPage + extendPage - 1; } else { endPage = countPage; startPage = endPage - extendPage + 1; if (curPage >= countPage) t2 = "<a class=\"disabled\" title=\"下一页\" href=\"javascript:;\">下一页</a><a class=\"disabled\" title=\"尾页\" href=\"javascript:;\">尾页</a>"; } } else { endPage = extendPage; if (curPage <= 1 || countPage == 1) //t1 = "<a class=\"disabled\" title=\"首页\" href=\"javascript:;" + "\">首页</a><a class=\"disabled\" title=\"上一页\" href=\"javascript:;\">上一页</a>"; t1 = "<a href=\"javascript:;\" class=\"layui-laypage-prev layui-disabled\" data-page=\"0\">上一页</a>"; } } else { startPage = 1; endPage = countPage; if (curPage <= 1 || countPage == 1) t1 = "<a class=\"disabled layui-disabled\" title=\"首页\" href=\"javascript:;" + "\">首页</a><a class=\"disabled layui-disabled\" title =\"上一页\" href=\"javascript:;\">上一页</a>"; if (curPage >= countPage) t2 = "<a class=\"disabled layui-disabled\" title=\"下一页\" href=\"javascript:;\">下一页</a><a class=\"disabled layui-disabled\" title=\"尾页\" href=\"javascript:;\">尾页</a>"; } StringBuilder s = new StringBuilder(""); var sdev = "<div class=\"flex-end\" id=\"paging\"><div class=\"layui-box layui-laypage layui-laypage-default\" id=\"layui-laypage-1\">"; if (isInstitutional) { s.Append(sdev); } s.Append(t1); if (curPage > (extendPage / 2) + 1) t3 = "<a title=\"上翻\" href=\"javascript:;\" onclick=\"" + url.Replace("{0}", startPage.ToString()) + "\">...</a>"; s.Append(t3); for (int i = startPage; i <= endPage; i++) { if (i == curPage) { //s.Append("<a class=\"active\" href=\"javascript:;\" title=\"第 " + i + " 页\"> "); //s.Append(i); //s.Append("</a>"); s.Append("<span class=\"layui-laypage-curr\"><em class=\"layui-laypage-em\"></em><em>" + i + "</em></span>"); } else { s.Append("<a title=\"第 " + i + " 页\" href=\"javascript:;\""); s.Append(" onclick=\""); s.Append(url.Replace("{0}", i.ToString())); s.Append("\">"); s.Append(i); s.Append("</a>"); //s.Append("<span class=\"layui-laypage-curr\"><em class=\"layui-laypage-em\"></em><em>" + i + "</em></span>"); } } if (curPage + (extendPage / 2) < countPage) t4 = "<a title=\"下翻\" href=\"javascript:;\" onclick=\"" + url.Replace("{0}", endPage.ToString()) + "\">...</a>"; s.Append(t4); s.Append(t2); var sdev1 = "</div></div>"; if (isInstitutional) { s.Append(sdev1); } return s.ToString(); } /// <summary> /// 获得页码显示链接 /// </summary> /// <param name="curPage">当前页数</param> /// <param name="countPage">总页数</param> /// <param name="url">超级链接地址</param> /// <param name="extendPage">周边页码显示个数上限</param> /// <returns>页码html</returns> public static string GetPageNumbers(int curPage, int countPage, string url, int extendPage) { int startPage = 1; int endPage = 1; if (url.IndexOf("?") == -1) { url = url + "?"; } else { url = url + "&"; } string t1 = "<a title=\"首页\" href=\"" + url + "page=1" + "\">首页</a><a title=\"上一页\" href=\"" + url + "page=" + (curPage - 1) + "\">上一页</a>"; string t2 = "<a title=\"下一页\" href=\"" + url + "page=" + (curPage + 1) + "\">下一页</a><a title=\"尾页\" href=\"" + url + "page=" + countPage + "\">尾页</a>"; string t3 = ""; string t4 = ""; if (countPage < 1) countPage = 1; if (extendPage < 3) extendPage = 2; if (countPage > extendPage) { if (curPage - (extendPage / 2) > 0) { if (curPage + (extendPage / 2) < countPage) { startPage = curPage - (extendPage / 2); endPage = startPage + extendPage - 1; } else { endPage = countPage; startPage = endPage - extendPage + 1; if (curPage >= countPage) t2 = "<a class=\"disabled\" title=\"下一页\" href=\"javascript:;\">下一页</a><a class=\"disabled\" title=\"尾页\" href=\"javascript:;\">尾页</a>"; } } else { endPage = extendPage; if(curPage <= 1 || countPage == 1) t1 = "<a class=\"disabled\" title=\"首页\" href=\"javascript:;" + "\">首页</a><a class=\"disabled\" title=\"上一页\" href=\"javascript:;\">上一页</a>"; } } else { startPage = 1; endPage = countPage; if (curPage <= 1 || countPage == 1) t1 = "<a class=\"disabled\" title=\"首页\" href=\"javascript:;" + "\">首页</a><a class=\"disabled\" title=\"上一页\" href=\"javascript:;\">上一页</a>"; if (curPage >= countPage) t2 = "<a class=\"disabled\" title=\"下一页\" href=\"javascript:;\">下一页</a><a class=\"disabled\" title=\"尾页\" href=\"javascript:;\">尾页</a>"; } StringBuilder s = new StringBuilder(""); s.Append(t1); if (curPage > (extendPage / 2) + 1) t3 = "<a title=\"上翻\" href=\"" + url + "page=" + startPage + "\">...</a>"; s.Append(t3); for (int i = startPage; i <= endPage; i++) { if (i == curPage) { s.Append("<a class=\"active\" href=\"javascript:;\" title=\"第 " + i + " 页\"> "); s.Append(i); s.Append("</a>"); } else { s.Append("<a title=\"第 " + i + " 页\" href=\""); s.Append(url); s.Append("page="); s.Append(i); s.Append("\">"); s.Append(i); s.Append("</a>"); } } if (curPage + (extendPage / 2) < countPage) t4= "<a title=\"下翻\" href=\"" + url + "page=" + endPage + "\">...</a>"; s.Append(t4); s.Append(t2); return s.ToString(); } /// <summary> /// blog 分页 /// </summary> /// <param name="curPage"></param> /// <param name="countPage"></param> /// <param name="url"></param> /// <param name="extendPage"></param> /// <returns></returns> public static string GetBlogPageNumbers(int curPage, int countPage, string url, int extendPage) { int startPage = 1; int endPage = 1; string t1 = "<a class=\"begin\" title=\"首页\" href=\"" + url.Replace("{page}", "1") + "\"><</a> "; string t2 = "<a class=\"end\" title=\"尾页\" href=\"" + url.Replace("{page}", countPage.ToString()) + "\">></a>"; if (countPage < 1) countPage = 1; if (extendPage < 3) extendPage = 2; if (countPage > extendPage) { if (curPage - (extendPage / 2) > 0) { if (curPage + (extendPage / 2) < countPage) { startPage = curPage - (extendPage / 2); endPage = startPage + extendPage - 1; } else { endPage = countPage; startPage = endPage - extendPage + 1; t2 = ""; } } else { endPage = extendPage; t1 = ""; } } else { startPage = 1; endPage = countPage; t1 = ""; t2 = ""; } StringBuilder s = new StringBuilder(""); s.Append(t1); for (int i = startPage; i <= endPage; i++) { if (i == curPage) { s.Append("<a title=\"第 " + i + " 页\" href=\"javascript:;\" class=\"current\">"); s.Append(i); s.Append("</a> "); } else { s.Append("<a title=\"第 " + i + " 页\" href=\""); s.Append(url.Replace("{page}", i.ToString())); s.Append("\">"); s.Append(i); s.Append("</a> "); } } s.Append(t2); return s.ToString(); }
使用
GetPageNumbers((Int32)Model.CurrentPage, (Int32)Model.TotalPages, "**/**?state=" + GetRequestInt("state", -1)+"......." )
后面的省略号可以拼接添加多个参数
标签:控件,extendPage,endPage,c#,int,MVC,curPage,countPage,Append From: https://www.cnblogs.com/wuyabaibsd/p/17727237.html