PagerHelper.cs

    1 /*
    2  ASP.NET MvcPager control
    3  Copyright:2009-2010 Webdiyer (http://en.webdiyer.com)
    4  Source code released under Ms-PL license
    5  */
    1 using System.Collections.Generic;
    2 using System.Web.Mvc;
    3 using System.Web.Mvc.Ajax;
    4 using System.Web.Routing;
    5 
    6 namespace Webdiyer.WebControls.Mvc
    7 {
    8     public static class PagerHelper
    9     {
   10         #region Html Pager
   11         public static MvcHtmlString Pager(this HtmlHelper helper, int totalPageCount, int pageIndex, string actionName, string controllerName, 
   12             PagerOptions pagerOptions, string routeName, object routeValues, object htmlAttributes)
   13         {
   14             var builder = new PagerBuilder
   15                 (
   16                     helper,
   17                     actionName,
   18                     controllerName,
   19                     totalPageCount,
   20                     pageIndex,
   21                     pagerOptions,
   22                     routeName,
   23                     new RouteValueDictionary(routeValues),
   24                     new RouteValueDictionary(htmlAttributes)
   25                 );
   26             return builder.RenderPager();
   27         }
   28 
   29         public static MvcHtmlString Pager(this HtmlHelper helper, int totalPageCount, int pageIndex, string actionName, string controllerName, 
   30             PagerOptions pagerOptions, string routeName, RouteValueDictionary routeValues, IDictionary<string, object> htmlAttributes)
   31         {
   32             var builder = new PagerBuilder
   33                 (
   34                     helper,
   35                     actionName,
   36                     controllerName,
   37                     totalPageCount,
   38                     pageIndex,
   39                     pagerOptions,
   40                     routeName,
   41                     routeValues,
   42                     htmlAttributes
   43                 );
   44             return builder.RenderPager();
   45         }
   46 
   47         private static MvcHtmlString Pager(HtmlHelper helper,PagerOptions pagerOptions,IDictionary<string,object> htmlAttributes)
   48         {
   49             return new PagerBuilder(helper, null, pagerOptions, htmlAttributes).RenderPager();
   50         }
   51 
   52         public static MvcHtmlString Pager<T>(this HtmlHelper helper, PagedList<T> pagedList)
   53         {
   54             if (pagedList == null)
   55                 return Pager(helper,null, null);
   56             return Pager(helper, pagedList.TotalPageCount, pagedList.CurrentPageIndex, null, null, null,null, null,null);
   57         }
   58 
   59         public static MvcHtmlString Pager<T>(this HtmlHelper helper, PagedList<T> pagedList, PagerOptions pagerOptions)
   60         {
   61             if(pagedList==null)
   62                 return Pager(helper,pagerOptions,null);
   63             return Pager(helper, pagedList.TotalPageCount, pagedList.CurrentPageIndex, null, null, pagerOptions,null, null,null);
   64         }
   65 
   66         public static MvcHtmlString Pager<T>(this HtmlHelper helper, PagedList<T> pagedList, PagerOptions pagerOptions, object htmlAttributes)
   67         {
   68             if (pagedList == null)
   69                 return Pager(helper,pagerOptions,new RouteValueDictionary(htmlAttributes));
   70             return Pager(helper, pagedList.TotalPageCount, pagedList.CurrentPageIndex, null, null, pagerOptions, null, null,htmlAttributes);
   71         }
   72 
   73         public static MvcHtmlString Pager<T>(this HtmlHelper helper, PagedList<T> pagedList, PagerOptions pagerOptions, IDictionary<string, object> htmlAttributes)
   74         {
   75             if (pagedList == null)
   76                 return Pager(helper,pagerOptions, htmlAttributes);
   77             return Pager(helper, pagedList.TotalPageCount, pagedList.CurrentPageIndex, null, null, pagerOptions, null, null, htmlAttributes);
   78         }
   79 
   80         public static MvcHtmlString Pager<T>(this HtmlHelper helper, PagedList<T> pagedList, PagerOptions pagerOptions, string routeName, object routeValues)
   81         {
   82             if (pagedList == null)
   83                 return Pager(helper,pagerOptions,null);
   84             return Pager(helper, pagedList.TotalPageCount, pagedList.CurrentPageIndex, null, null, pagerOptions, routeName, routeValues, null);
   85         }
   86 
   87         public static MvcHtmlString Pager<T>(this HtmlHelper helper, PagedList<T> pagedList, PagerOptions pagerOptions, string routeName, RouteValueDictionary routeValues)
   88         {
   89             if (pagedList == null)
   90                 return Pager(helper,pagerOptions,null);
   91             return Pager(helper, pagedList.TotalPageCount, pagedList.CurrentPageIndex, null, null, pagerOptions, routeName, routeValues, null);
   92         }
   93 
   94         public static MvcHtmlString Pager<T>(this HtmlHelper helper, PagedList<T> pagedList, PagerOptions pagerOptions, string routeName, object routeValues, object htmlAttributes)
   95         {
   96             if (pagedList == null)
   97                 return Pager(helper,pagerOptions,new RouteValueDictionary(htmlAttributes));
   98             return Pager(helper, pagedList.TotalPageCount, pagedList.CurrentPageIndex, null, null, pagerOptions, routeName, routeValues, htmlAttributes);
   99         }
  100 
  101         public static MvcHtmlString Pager<T>(this HtmlHelper helper, PagedList<T> pagedList, PagerOptions pagerOptions, string routeName, 
  102             RouteValueDictionary routeValues, IDictionary<string, object> htmlAttributes)
  103         {
  104             if (pagedList == null)
  105                 return Pager(helper,pagerOptions,htmlAttributes);
  106             return Pager(helper, pagedList.TotalPageCount, pagedList.CurrentPageIndex, null, null, pagerOptions, routeName, routeValues, htmlAttributes);
  107         }
  108 
  109         public static MvcHtmlString Pager<T>(this HtmlHelper helper, PagedList<T> pagedList, string routeName, object routeValues, object htmlAttributes)
  110         {
  111             if (pagedList == null)
  112                 return Pager(helper,null,new RouteValueDictionary(htmlAttributes));
  113             return Pager(helper, pagedList.TotalPageCount, pagedList.CurrentPageIndex, null, null, null, routeName, routeValues, htmlAttributes);
  114         }
  115 
  116         public static MvcHtmlString Pager<T>(this HtmlHelper helper, PagedList<T> pagedList, string routeName, RouteValueDictionary routeValues, 
  117             IDictionary<string, object> htmlAttributes)
  118         {
  119             if (pagedList == null)
  120                 return Pager(helper,null, htmlAttributes);
  121             return Pager(helper, pagedList.TotalPageCount, pagedList.CurrentPageIndex, null, null, null, routeName, routeValues, htmlAttributes);
  122         }
  123 
  124         #endregion
  125 
  126         #region jQuery Ajax Pager
  127 
  128         private static MvcHtmlString AjaxPager(HtmlHelper html, PagerOptions pagerOptions, IDictionary<string, object> htmlAttributes)
  129         {
  130             return new PagerBuilder(html,null, pagerOptions, htmlAttributes).RenderPager();
  131         }
  132 
  133         public static MvcHtmlString AjaxPager(this HtmlHelper html, int totalPageCount, int pageIndex, string actionName, string controllerName, 
  134             string routeName, PagerOptions pagerOptions, object routeValues, AjaxOptions ajaxOptions, object htmlAttributes)
  135         {
  136             if (pagerOptions == null)
  137                 pagerOptions = new PagerOptions();
  138             pagerOptions.UseJqueryAjax = true;
  139 
  140             var builder = new PagerBuilder(html, actionName, controllerName, totalPageCount, pageIndex, pagerOptions,
  141                                            routeName, new RouteValueDictionary(routeValues), ajaxOptions, new RouteValueDictionary(htmlAttributes));
  142             return builder.RenderPager();
  143         }
  144 
  145         public static MvcHtmlString AjaxPager(this HtmlHelper html, int totalPageCount, int pageIndex, string actionName, string controllerName, 
  146             string routeName, PagerOptions pagerOptions, RouteValueDictionary routeValues, AjaxOptions ajaxOptions, IDictionary<string, object> htmlAttributes)
  147         {
  148             if (pagerOptions == null)
  149                 pagerOptions = new PagerOptions();
  150             pagerOptions.UseJqueryAjax = true;
  151 
  152             var builder = new PagerBuilder(html, actionName, controllerName, totalPageCount, pageIndex, pagerOptions,
  153                                            routeName, routeValues, ajaxOptions, htmlAttributes);
  154             return builder.RenderPager();
  155         }
  156 
  157         public static MvcHtmlString AjaxPager<T>(this HtmlHelper html, PagedList<T> pagedList, AjaxOptions ajaxOptions)
  158         {
  159             if (pagedList == null)
  160                 return AjaxPager(html,null,null);
  161             return AjaxPager(html, pagedList.TotalPageCount, pagedList.CurrentPageIndex, null, null, null, null, null, ajaxOptions,
  162                              null);
  163         }
  164 
  165         public static MvcHtmlString AjaxPager<T>(this HtmlHelper html, PagedList<T> pagedList, string routeName, AjaxOptions ajaxOptions)
  166         {
  167             if (pagedList == null)
  168                 return AjaxPager(html,null,null);
  169             return AjaxPager(html, pagedList.TotalPageCount, pagedList.CurrentPageIndex, null, null, routeName, null, null, ajaxOptions,
  170                              null);
  171         }
  172 
  173         public static MvcHtmlString AjaxPager<T>(this HtmlHelper html, PagedList<T> pagedList, PagerOptions pagerOptions, AjaxOptions ajaxOptions)
  174         {
  175             if(pagedList==null)
  176                 return AjaxPager(html,pagerOptions,null);
  177             return AjaxPager(html, pagedList.TotalPageCount, pagedList.CurrentPageIndex, null, null, null, pagerOptions, null, ajaxOptions,
  178                              null);
  179         }
  180 
  181         public static MvcHtmlString AjaxPager<T>(this HtmlHelper html, PagedList<T> pagedList, PagerOptions pagerOptions, AjaxOptions ajaxOptions, object htmlAttributes)
  182         {
  183             if (pagedList == null)
  184                 return AjaxPager(html,pagerOptions,new RouteValueDictionary(htmlAttributes));
  185             return AjaxPager(html, pagedList.TotalPageCount, pagedList.CurrentPageIndex, null, null, null, pagerOptions, null,
  186                              ajaxOptions, htmlAttributes);
  187         }
  188 
  189         public static MvcHtmlString AjaxPager<T>(this HtmlHelper html, PagedList<T> pagedList, PagerOptions pagerOptions, AjaxOptions ajaxOptions, 
  190             IDictionary<string, object> htmlAttributes)
  191         {
  192             if (pagedList == null)
  193                 return AjaxPager(html,pagerOptions,htmlAttributes);
  194             return AjaxPager(html, pagedList.TotalPageCount, pagedList.CurrentPageIndex, null, null, null, pagerOptions, null,
  195                              ajaxOptions, htmlAttributes);
  196         }
  197 
  198         public static MvcHtmlString AjaxPager<T>(this HtmlHelper html, PagedList<T> pagedList, string routeName, object routeValues, PagerOptions pagerOptions, AjaxOptions ajaxOptions)
  199         {
  200             if (pagedList == null)
  201                 return AjaxPager(html,pagerOptions,null);
  202             return AjaxPager(html, pagedList.TotalPageCount, pagedList.CurrentPageIndex, null, null, routeName, pagerOptions, routeValues, ajaxOptions,
  203                              null);
  204         }
  205 
  206         public static MvcHtmlString AjaxPager<T>(this HtmlHelper html, PagedList<T> pagedList, string routeName, object routeValues, 
  207             PagerOptions pagerOptions, AjaxOptions ajaxOptions, object htmlAttributes)
  208         {
  209             if (pagedList == null)
  210                 return AjaxPager(html,pagerOptions,new RouteValueDictionary(htmlAttributes));
  211             return AjaxPager(html, pagedList.TotalPageCount, pagedList.CurrentPageIndex, null, null, routeName, pagerOptions,
  212                              routeValues, ajaxOptions, htmlAttributes);
  213         }
  214 
  215         public static MvcHtmlString AjaxPager<T>(this HtmlHelper html, PagedList<T> pagedList, string routeName, RouteValueDictionary routeValues, 
  216             PagerOptions pagerOptions, AjaxOptions ajaxOptions, IDictionary<string, object> htmlAttributes)
  217         {
  218             if (pagedList == null)
  219                 return AjaxPager(html,pagerOptions,htmlAttributes);
  220             return AjaxPager(html, pagedList.TotalPageCount, pagedList.CurrentPageIndex, null, null, routeName, pagerOptions,
  221                              routeValues, ajaxOptions, htmlAttributes);
  222         }
  223 
  224         public static MvcHtmlString AjaxPager<T>(this HtmlHelper html, PagedList<T> pagedList, string actionName, string controllerName, 
  225             PagerOptions pagerOptions, AjaxOptions ajaxOptions)
  226         {
  227             if (pagedList == null)
  228                 return AjaxPager(html,pagerOptions,null);
  229             return AjaxPager(html, pagedList.TotalPageCount, pagedList.CurrentPageIndex, actionName, controllerName, null, pagerOptions, null, ajaxOptions,
  230                              null);
  231         }
  232 
  233         #endregion
  234 
  235         #region Microsoft Ajax Pager
  236 
  237         public static MvcHtmlString Pager(this AjaxHelper ajax, int totalPageCount, int pageIndex, string actionName, string controllerName, 
  238             string routeName, PagerOptions pagerOptions, object routeValues, AjaxOptions ajaxOptions, object htmlAttributes)
  239         {
  240             var builder = new PagerBuilder(ajax, actionName, controllerName, totalPageCount, pageIndex, pagerOptions,
  241                                            routeName, new RouteValueDictionary(routeValues), ajaxOptions, new RouteValueDictionary(htmlAttributes));
  242             return builder.RenderPager();
  243         }
  244 
  245         public static MvcHtmlString Pager(this AjaxHelper ajax, int totalPageCount, int pageIndex, string actionName, string controllerName, 
  246             string routeName, PagerOptions pagerOptions, RouteValueDictionary routeValues, AjaxOptions ajaxOptions, IDictionary<string, object> htmlAttributes)
  247         {
  248             var builder = new PagerBuilder(ajax, actionName, controllerName, totalPageCount, pageIndex, pagerOptions,
  249                                            routeName, routeValues, ajaxOptions, htmlAttributes);
  250             return builder.RenderPager();
  251         }
  252 
  253         private static MvcHtmlString Pager(AjaxHelper ajax, PagerOptions pagerOptions, IDictionary<string, object> htmlAttributes)
  254         {
  255             return new PagerBuilder(null, ajax, pagerOptions, htmlAttributes).RenderPager();
  256         }
  257 
  258         public static MvcHtmlString Pager<T>(this AjaxHelper ajax, PagedList<T> pagedList, AjaxOptions ajaxOptions)
  259         {
  260             return pagedList==null ? Pager(ajax,null, null) : Pager(ajax,pagedList.TotalPageCount,pagedList.CurrentPageIndex, null, null, null, null, null, ajaxOptions, null);
  261         }
  262 
  263         public static MvcHtmlString Pager<T>(this AjaxHelper ajax, PagedList<T> pagedList, PagerOptions pagerOptions, AjaxOptions ajaxOptions)
  264         {
  265             return pagedList == null ? Pager(ajax,pagerOptions,null) : Pager(ajax, pagedList.TotalPageCount, pagedList.CurrentPageIndex, 
  266                 null, null, null,pagerOptions, null, ajaxOptions, null);
  267         }
  268 
  269         public static MvcHtmlString Pager<T>(this AjaxHelper ajax, PagedList<T> pagedList, PagerOptions pagerOptions, AjaxOptions ajaxOptions, object htmlAttributes)
  270         {
  271             if(pagedList==null)
  272                 return Pager(ajax,pagerOptions,new RouteValueDictionary(htmlAttributes));
  273             return Pager(ajax, pagedList.TotalPageCount, pagedList.CurrentPageIndex, null, null, null, pagerOptions, null, ajaxOptions, htmlAttributes);
  274         }
  275 
  276         public static MvcHtmlString Pager<T>(this AjaxHelper ajax, PagedList<T> pagedList, PagerOptions pagerOptions, AjaxOptions ajaxOptions, IDictionary<string, object> htmlAttributes)
  277         {
  278             if (pagedList == null)
  279                 return Pager(ajax,pagerOptions,htmlAttributes);
  280             return Pager(ajax, pagedList.TotalPageCount, pagedList.CurrentPageIndex, null, null, null, pagerOptions, null, ajaxOptions, htmlAttributes);
  281         }
  282 
  283         public static MvcHtmlString Pager<T>(this AjaxHelper ajax, PagedList<T> pagedList, string routeName, object routeValues, AjaxOptions ajaxOptions, object htmlAttributes)
  284         {
  285             if(pagedList==null)
  286                 return Pager(ajax,null,new RouteValueDictionary(htmlAttributes));
  287             return Pager(ajax,pagedList.TotalPageCount,pagedList.CurrentPageIndex, null, null, routeName, null, routeValues, ajaxOptions, htmlAttributes);
  288         }
  289 
  290         public static MvcHtmlString Pager<T>(this AjaxHelper ajax, PagedList<T> pagedList, string routeName, RouteValueDictionary routeValues, 
  291             AjaxOptions ajaxOptions, IDictionary<string, object> htmlAttributes)
  292         {
  293             if(pagedList==null)
  294                 return Pager(ajax,null, htmlAttributes);
  295             return Pager(ajax, pagedList.TotalPageCount, pagedList.CurrentPageIndex, null, null, routeName, null, routeValues, ajaxOptions, htmlAttributes);
  296         }
  297 
  298         public static MvcHtmlString Pager<T>(this AjaxHelper ajax, PagedList<T> pagedList, string routeName, object routeValues, PagerOptions pagerOptions, 
  299             AjaxOptions ajaxOptions, object htmlAttributes)
  300         {
  301             if(pagedList==null)
  302                 return Pager(ajax,pagerOptions,new RouteValueDictionary(htmlAttributes));
  303             return Pager(ajax, pagedList.TotalPageCount, pagedList.CurrentPageIndex, null, null, routeName, pagerOptions, routeValues, ajaxOptions, htmlAttributes);
  304         }
  305 
  306         public static MvcHtmlString Pager<T>(this AjaxHelper ajax, PagedList<T> pagedList, string routeName, RouteValueDictionary routeValues, 
  307             PagerOptions pagerOptions, AjaxOptions ajaxOptions, IDictionary<string, object> htmlAttributes)
  308         {
  309             if (pagedList == null)
  310                 return Pager(ajax,pagerOptions,htmlAttributes);
  311             return Pager(ajax, pagedList.TotalPageCount, pagedList.CurrentPageIndex, null, null, routeName, pagerOptions, routeValues, ajaxOptions, htmlAttributes);
  312         }
  313         #endregion
  314     }
  315 }