一个综合的MVC分页Helper

发个自写的综合分页Helper。可支持输入数字分页、使用存储过程分页、设置CSS样式。

Helper代码:

View Code
1 /// <summary>
2 /// 分页Helper样式
3 /// </summary>
4   public enum BarStyle
5 {
6 yahoo, digg, meneame, flickr, sabrosus, scott, quotes, black, black2, grayr, yellow, jogger, starcraft2, tres, megas512, technorati, youtube, msdn, badoo, viciao, yahoo2, green_black
7 }
8
9 /// <summary>
10 /// 分页关键数量参数
11 /// </summary>
12   public class PageX
13 {
14 /// <summary>
15 /// 总页数
16 /// </summary>
17   public int TotalPages { get; set; }
18
19 /// <summary>
20 /// 记录总数
21 /// </summary>
22   public int TotalCount { get; set; }
23
24 /// <summary>
25 /// 当前页
26 /// </summary>
27   public int PageIndex { get; set; }
28
29 /// <summary>
30 /// 页面显显记录数量
31 /// </summary>
32   public int PageSize { get; set; }
33
34 /// <summary>
35 /// 关键字
36 /// </summary>
37   public string Keyword { get; set; }
38 //public bool IsPreviousPage { get; set; }
39 //public bool IsNextPage { get; set; }
40  
41 public PageX(int pageIndex, int pageSize, int rowCount, string keyword)
42 {
43 PageIndex = pageIndex;
44 PageSize = pageSize;
45 TotalCount = rowCount;
46 Keyword = keyword;
47
48 TotalPages = (int)Math.Round((decimal)TotalCount / (decimal)PageSize);
49 }
50
51 public PageX(int pageIndex, int pageSize, int rowCount,
52 string keyword, bool isPreviousPage, bool isNextPage)
53 {
54 PageIndex = pageIndex;
55 PageSize = pageSize;
56 TotalCount = rowCount;
57 Keyword = keyword;
58 if (isPreviousPage)
59 PageIndex -= 1;
60 if (isNextPage)
61 PageIndex += 1;
62
63 TotalPages = (int)Math.Round((decimal)TotalCount / (decimal)PageSize);
64 }
65 }
66
67 /// <summary>
68 /// 翻页连接文字
69 /// </summary>
70   public class BtnStyle
71 {
72 public string First { get; set; }
73 public string Last { get; set; }
74 public string Next { get; set; }
75 public string Previous { get; set; }
76 }
77
78  public static class PagerZExtensions
79 {
80 public static string PagerZ(this HtmlHelper html, PageX pageX)
81 {
82 return PagerZ(html, pageX, null, null, BarStyle.scott);
83 }
84
85 public static string PagerZ(this HtmlHelper html, PageX pageX, BarStyle style)
86 {
87 return PagerZ(html, pageX, null, null, style);
88 }
89
90 public static string PagerZ(this HtmlHelper html, PageX pageX, Dictionary<string, object> parameters, BarStyle style)
91 {
92 return PagerZ(html, pageX, parameters, null, style);
93 }
94
95 public static string PagerZ(this HtmlHelper html, PageX pageX, Dictionary<string, object> parameters, BtnStyle btnStyle, BarStyle style)
96 {
97 int show = 4;
98 if (btnStyle == null)
99 {
100 btnStyle = new BtnStyle();
101 if (string.IsNullOrEmpty(btnStyle.First))
102 {
103 btnStyle.First = "<<";
104 }
105 if (string.IsNullOrEmpty(btnStyle.Last))
106 {
107 btnStyle.Last = ">>";
108 }
109 if (string.IsNullOrEmpty(btnStyle.Next))
110 {
111 btnStyle.Next = ">";
112 }
113 if (string.IsNullOrEmpty(btnStyle.Previous))
114 {
115 btnStyle.Previous = "<";
116 }
117 }
118 if (pageX.TotalPages == 1)
119 {
120 return "";
121 }
122 else
123 {
124 StringBuilder sb = new StringBuilder();
125 string _path = html.ViewContext.HttpContext.Request.Path;
126 sb.Append("<div class=\"");
127 sb.Append(style.ToString());
128 sb.Append("\" >");
129
130 string queryString = html.ViewContext.HttpContext.Request.QueryString.ToString();
131 if (queryString.IndexOf("page=") < 0)
132 {
133 queryString += "&page=" + pageX.PageIndex;
134 }
135
136 //if (parameters != null)
137 //{
138 // foreach (var par in parameters)
139 // {
140 // string findStr = par.Key + "=";
141 // if (queryString.IndexOf(findStr) < 0)
142 // {
143 // queryString += "&" + par.Key + "=" + par.Value;
144 // }
145 // }
146 //}
147  
148 Regex re = new Regex(@"page=\d+", RegexOptions.IgnoreCase);
149 string result = re.Replace(queryString, "page={0}");
150
151 if (pageX.PageIndex != 1)
152 {
153 sb.AppendFormat("<span><a href=\"{0}\" title=\"第一页\">{1}</a></span>", _path + "?" + string.Format(result, 1), btnStyle.First);
154 sb.AppendFormat("<span><a href=\"{0}\" title=\"上一页\">{1}</a></span>", _path + "?" + string.Format(result, pageX.PageIndex - 1), btnStyle.Previous);
155 }
156 if (pageX.PageIndex > (show + 1))
157 {
158 sb.AppendFormat("<span><a href=\"{0}\" title=\"前" + (show + 1) + "页\">{1}</a></span>", _path + "?" + string.Format(result, pageX.PageIndex - (show + 1)), "..");
159
160 }
161 for (int i = pageX.PageIndex - show; i <= pageX.PageIndex + show; i++)
162 {
163 if (i == pageX.PageIndex)
164 {
165 sb.AppendFormat("<span class=\"current\">{0}</span>", i);
166 }
167 else
168 {
169 if (i > 0 & i <= pageX.TotalPages)
170 {
171 sb.AppendFormat("<span><a href=\"{0}\">{1}</a></span>", _path + "?" + string.Format(result, i), i);
172 }
173 }
174 }
175 if (pageX.PageIndex < (pageX.TotalPages - (show)))
176 {
177 sb.AppendFormat("<span><a href=\"{0}\" title=\"后" + (show + 1) + "页\">{1}</a></span>", _path + "?" + string.Format(result, pageX.PageIndex + (show + 1)), "..");
178
179 }
180 if (pageX.PageIndex < pageX.TotalPages)
181 {
182 sb.AppendFormat("<span><a href=\"{0}\" title=\"下一页\">{1}</a></span>", _path + "?" + string.Format(result, pageX.PageIndex + 1), btnStyle.Next);
183 sb.AppendFormat("<span><a href=\"{0}\" title=\"最后一页\">{1}</a></span>", _path + "?" + string.Format(result, pageX.TotalPages), btnStyle.Last);
184
185 }
186 sb.AppendFormat("<span class=\"total\">共{1}页</span>", pageX.PageIndex, pageX.TotalPages);
187 sb.AppendFormat("<form id=\"filterPageForm\" name=\"filterPageForm\" method=\"get\" action=\"{0}\">", _path);
188 sb.AppendFormat("<input type=\"hidden\" name=\"keywords\" id=\"keywords\" value=\"{0}\"/>", pageX.Keyword);
189 if (parameters != null)
190 {
191 foreach (var p in parameters)
192 {
193 sb.AppendFormat("<input type=\"hidden\" name=\"{0}\" id=\"{0}\" value=\"{1}\"/>", p.Key, p.Value);
194
195 }
196 }
197 sb.AppendFormat("到第<input class=\"page\" type=\"text\" name=\"page\" id=\"page\" value=\"{0}\"/>页<input class=\"but\" type=\"submit\" value=\"确定\" />", pageX.PageIndex);
198 sb.Append("</form>");
199 sb.Append("</div>");
200 return sb.ToString();
201 }
202 }
203 }

ViewDataModel:

public class ViewTestTable
    {
        public IList<TestTable> TestTables { get; set; }

        public PageX PageX { get; set; }

        public Dictionary<string, object> PageParameters { get; set; }

        public BtnStyle BtnStyle { get; set; }

        #region 类构造
        private ViewTestTable() { }

        public static ViewTestTable Instance()
        {
            return new ViewTestTable();
        }
        #endregion
    }

Controller:

1public class TestController : Controller
2 {
3 //
4 // GET: /Test/
5 private const int _pageSize = 40;
6 private ViewTestTable _viewData;
7
8 public TestController()
9 {
10 this._viewData = ViewTestTable.Instance();
11 }
12
13 public ActionResult Index([DefaultValue(null)]string keywords, [DefaultValue(1)]int page,
14 [DefaultValue(0)]int minPrice, [DefaultValue(0)]int maxPrice, [DefaultValue("Default")]string proOrder,
15 [DefaultValue("List")]string Show)
16 {
17 //设置分页存储过程参数
18 var p = new ProParameter();
19 p.TableName = "tb_TestTable";
20 p.OrderFieldName = "Id";
21 p.OrderType = 0;
22 p.SelectFieldName = "*";
23 p.StrWhere = "";
24
25 int rowCount;
26 //调用分页存储过程
27 IList<TestTable> query = ExecProcedure.GetFromPage<TestTable>(p.TableName, p.SelectFieldName,
28 p.StrWhere, p.OrderFieldName, _pageSize, page, out rowCount, p.OrderType);
29
30 var pages = new PageX(page, _pageSize, rowCount, null);
31 var pagePar = new Dictionary<string, object>();
32 pagePar.Add("minPrice", minPrice);
33 pagePar.Add("maxPrice", maxPrice);
34 pagePar.Add("proOrder", proOrder);
35 pagePar.Add("Show", Show);
36
37 var btnStyle = new BtnStyle() { First="首页", Last="末页", Next="下一页", Previous="上一页" };
38
39 this._viewData.TestTables = query;
40 this._viewData.PageX = pages;
41 this._viewData.PageParameters = pagePar;
42 this._viewData.BtnStyle = btnStyle;
43 return View(this._viewData);
44 }
45}

View代码:

1<%@ Page Title="" Language="C#" MasterPageFile="~/Views/Shared/Site.Master" Inherits="System.Web.Mvc.ViewPage<dynamic>" %>
2
3<asp:Content ID="Content1" ContentPlaceHolderID="TitleContent" runat="server">
4 TestIndex
5</asp:Content>
6
7<asp:Content ID="Content2" ContentPlaceHolderID="MainContent" runat="server">
8<% foreach (var i in Model.TestTables)
9 {%>
10 <ul>
11 <li><%: i.Id %></li>
12 <li><%: i.UserName %></li>
13 <li><%: i.UserPWD %></li>
14 <li><%: i.UserEmail %></li>
15 </ul>
16<% }%>
17<% = Html.PagerZ((Model.PageX) as PageX) %>
18<% = Html.PagerZ((Model.PageX) as PageX, (Model.PageParameters) as Dictionary<string, object>, (Model.BtnStyle) as BtnStyle, BarStyle.scott)%>
19
20</asp:Content>

CSS样式:

这是样式之一,可以自已补充。

div.badoo{ padding: 5px 5px 5px 100px;margin-top: 10px;text-align: left;}
div.badoo a{border: 1px solid #97C0FF;padding: 2px 5px 2px 5px;margin-right: 8px;}
div.badoo span.current{border: 1px solid #BFC6C1;padding: 2px 5px 2px 5px;margin-right: 8px;background-color: #E6E8E7;}


div.scott
{
	padding: 5px;
	margin-top: 10px;
	text-align: left;
	line-height: 25px;
}
div.scott a{border: 1px solid #97C0FF;padding: 2px 5px 2px 5px;margin-right: 8px;}
div.scott span.current{border: 1px solid #BFC6C1;padding: 2px 5px 2px 5px;margin-right: 8px;background-color: #E6E8E7;}
div.scott span.total{padding: 2px 5px 2px 5px;margin-right: 5px;}
div.scott form{ padding:0px; margin:0px; display: inline;}
div.scott input.page{ width:25px; border: 1px #93bee2 solid; margin:0 5px 0 5px;}
div.scott input.but{ height:20px; width:48px;font-size: 9px; border: 1px #93bee2 solid; background-color: #FFFFFF; font-style: normal ; width: 60px; margin-left:5px;}

posted on 2011-04-19 10:48  Ω元素  阅读(942)  评论(3编辑  收藏  举报

导航