AspNetPager paging control

Helper.cs

    1 /* AspNetPager version 7.3.2
    2 Copyright (C) 2003-2010  Webdiyer(http://en.webdiyer.com)
    3 
    4 This file is part of AspNetPager.
    5 
    6 AspNetPager is free software: you can redistribute it and/or modify
    7 it under the terms of the GNU Lesser General Public License as published by
    8 the Free Software Foundation, either version 3 of the License, or
    9 (at your option) any later version.
   10 
   11 AspNetPager is distributed in the hope that it will be useful,
   12 but WITHOUT ANY WARRANTY; without even the implied warranty of
   13 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
   14 GNU Lesser General Public License for more details.
   15 
   16 You should have received a copy of the GNU Lesser General Public License
   17 along with AspNetPager.  If not, see <http://www.gnu.org/licenses/>.
   18 */
   17 
   18 using System;
   19 using System.Collections.Specialized;
   20 using System.IO;
   21 using System.Text;
   22 using System.Web.UI;
   23 using System.Web.UI.WebControls;
   24 using System.Text.RegularExpressions;
   25 
   26 namespace Wuqi.Webdiyer
   27 {
   28     public partial class AspNetPager
   29     {
   30         #region Private Helper Functions
   31 
   32         static void addMoreListItem(HtmlTextWriter writer, int pageIndex)
   33         {
   34             writer.Write("<option value=\"");
   35             writer.Write(pageIndex);
   36             writer.Write("\">......</option>");
   37         }
   38 
   39         void listPageIndices(HtmlTextWriter writer, int startIndex, int endIndex)
   40         {
   41             for (int i = startIndex; i <= endIndex; i++)
   42             {
   43                 writer.Write("<option value=\"");
   44                 writer.Write(i);
   45                 writer.Write("\"");
   46                 if (i == CurrentPageIndex)
   47                     writer.Write(" selected=\"true\"");
   48                 writer.Write(">");
   49                 writer.Write(i);
   50                 writer.Write("</option>");
   51             }
   52         }
   53 
   54         private void RenderCustomInfoSection(HtmlTextWriter writer)
   55         {
   56             if (Height != Unit.Empty)
   57                 writer.AddStyleAttribute(HtmlTextWriterStyle.Height, Height.ToString());
   58             string customUnit = CustomInfoSectionWidth.ToString();
   59             if (CustomInfoClass != null && CustomInfoClass.Trim().Length > 0)
   60                 writer.AddAttribute(HtmlTextWriterAttribute.Class, CustomInfoClass);
   61             if (CustomInfoStyle != null && CustomInfoStyle.Trim().Length > 0)
   62                 writer.AddAttribute(HtmlTextWriterAttribute.Style, CustomInfoStyle);
   63             writer.AddStyleAttribute(HtmlTextWriterStyle.Width, customUnit);
   64             if (CustomInfoTextAlign != HorizontalAlign.NotSet)
   65                 writer.AddAttribute(HtmlTextWriterAttribute.Align, CustomInfoTextAlign.ToString().ToLower());
   66             if (LayoutType == LayoutType.Div)
   67             {
   68                 writer.AddStyleAttribute("float", "left");
   69                 writer.RenderBeginTag(HtmlTextWriterTag.Div);
   70             }
   71             else
   72             {
   73                 writer.AddAttribute(HtmlTextWriterAttribute.Valign, "bottom");
   74                 writer.AddAttribute(HtmlTextWriterAttribute.Nowrap, "true");
   75                 writer.RenderBeginTag(HtmlTextWriterTag.Td);
   76             }
   77             writer.Write(GetCustomInfoHtml(CustomInfoHTML));
   78             writer.RenderEndTag();
   79         }
   80 
   81         private void RenderNavigationSection(HtmlTextWriter writer)
   82         {
   83             if (CustomInfoSectionWidth.Type == UnitType.Percentage)
   84             {
   85                 writer.AddStyleAttribute(HtmlTextWriterStyle.Width,
   86                                          (Unit.Percentage(100 - CustomInfoSectionWidth.Value)).ToString());
   87             }
   88             if (HorizontalAlign != HorizontalAlign.NotSet)
   89                 writer.AddAttribute(HtmlTextWriterAttribute.Align, HorizontalAlign.ToString().ToLower());
   90             if (!string.IsNullOrEmpty(CssClass))
   91                 writer.AddAttribute(HtmlTextWriterAttribute.Class, CssClass);
   92             if (LayoutType == LayoutType.Div)
   93             {
   94                 writer.AddStyleAttribute("float", "left");
   95                 writer.RenderBeginTag(HtmlTextWriterTag.Div); //<div>
   96             }
   97             else
   98             {
   99                 writer.AddAttribute(HtmlTextWriterAttribute.Valign, "bottom");
  100                 writer.AddAttribute(HtmlTextWriterAttribute.Nowrap, "true");
  101                 writer.RenderBeginTag(HtmlTextWriterTag.Td); //<td>
  102             }
  103             RenderPagingElements(writer);
  104             writer.RenderEndTag(); //</div> or </td>
  105         }
  106 
  107         private void RenderPagingElements(HtmlTextWriter writer)
  108         {
  109             int startIndex = ((CurrentPageIndex - 1)/NumericButtonCount)*NumericButtonCount;
  110                 //this is an important trick, it's not the same as CurrentPageIndex-1
  111             if (PageCount > NumericButtonCount && CurrentPageButtonPosition != PagingButtonPosition.Fixed)
  112             {
  113                 switch (CurrentPageButtonPosition)
  114                 {
  115                     case PagingButtonPosition.End:
  116                         if (CurrentPageIndex > NumericButtonCount)
  117                             startIndex = CurrentPageIndex - NumericButtonCount;
  118                         break;
  119                     case PagingButtonPosition.Center:
  120                         int startOffset = CurrentPageIndex - (int) (Math.Ceiling((double) NumericButtonCount/2));
  121                         if (startOffset > 0)
  122                         {
  123                             startIndex = startOffset;
  124                             if (startIndex > (PageCount - NumericButtonCount))
  125                                 startIndex = PageCount - NumericButtonCount;
  126                         }
  127                         break;
  128                     case PagingButtonPosition.Beginning:
  129                         startIndex = CurrentPageIndex - 1;
  130                         if (startIndex + NumericButtonCount > PageCount)
  131                             startIndex = PageCount - NumericButtonCount;
  132                         break;
  133                 }
  134             }
  135 
  136             int endIndex = ((startIndex + NumericButtonCount) > PageCount)
  137                                ? PageCount
  138                                : (startIndex + NumericButtonCount);
  139 
  140             if (PagingButtonLayoutType == PagingButtonLayoutType.UnorderedList)
  141                 writer.RenderBeginTag(HtmlTextWriterTag.Ul); //<ul>
  142 
  143             if (NavigationButtonsPosition == NavigationButtonPosition.Left ||
  144                 NavigationButtonsPosition == NavigationButtonPosition.BothSides)
  145             {
  146                 CreateNavigationButton(writer, NavigationButton.First);
  147                 CreateNavigationButton(writer, NavigationButton.Prev);
  148                 if (NavigationButtonsPosition == NavigationButtonPosition.Left)
  149                 {
  150                     CreateNavigationButton(writer, NavigationButton.Next);
  151                     CreateNavigationButton(writer, NavigationButton.Last);
  152                 }
  153             }
  154 
  155             if (AlwaysShowFirstLastPageNumber && startIndex > 1)
  156                 CreateNumericButton(writer, 1);
  157 
  158             if (ShowMoreButtons && startIndex > 0)
  159                 CreateMoreButton(writer, startIndex);
  160             if (ShowPageIndex)
  161             {
  162                 for (int i = startIndex + 1; i <= endIndex; i++)
  163                 {
  164                     CreateNumericButton(writer, i);
  165                 }
  166             }
  167             if (ShowMoreButtons && PageCount > NumericButtonCount && endIndex < PageCount)
  168                 CreateMoreButton(writer, endIndex + 1);
  169 
  170             if (AlwaysShowFirstLastPageNumber && endIndex < PageCount)
  171                 CreateNumericButton(writer, PageCount);
  172 
  173             if (NavigationButtonsPosition == NavigationButtonPosition.Right ||
  174                 NavigationButtonsPosition == NavigationButtonPosition.BothSides)
  175             {
  176                 if (NavigationButtonsPosition == NavigationButtonPosition.Right)
  177                 {
  178                     CreateNavigationButton(writer, NavigationButton.First);
  179                     CreateNavigationButton(writer, NavigationButton.Prev);
  180                 }
  181                 CreateNavigationButton(writer, NavigationButton.Next);
  182                 CreateNavigationButton(writer, NavigationButton.Last);
  183             }
  184 
  185             if (PagingButtonLayoutType == PagingButtonLayoutType.UnorderedList)
  186                 writer.RenderEndTag(); //</ul>
  187 
  188 
  189             if ((ShowPageIndexBox == ShowPageIndexBox.Always) ||
  190                 (ShowPageIndexBox == ShowPageIndexBox.Auto && PageCount >= ShowBoxThreshold))
  191             {
  192                 string boxClientId = UniqueID + "_input";
  193                 writer.Write("&nbsp;&nbsp;");
  194                 if (!string.IsNullOrEmpty(TextBeforePageIndexBox))
  195                     writer.Write(TextBeforePageIndexBox);
  196                 if (PageIndexBoxType == PageIndexBoxType.TextBox) //TextBox
  197                 {
  198                     writer.AddAttribute(HtmlTextWriterAttribute.Type, "text");
  199                     writer.AddStyleAttribute(HtmlTextWriterStyle.Width, "30px");
  200                     writer.AddAttribute(HtmlTextWriterAttribute.Value, CurrentPageIndex.ToString());
  201                     if (!string.IsNullOrEmpty(PageIndexBoxStyle))
  202                         writer.AddAttribute(HtmlTextWriterAttribute.Style, PageIndexBoxStyle);
  203                     if (!string.IsNullOrEmpty(PageIndexBoxClass))
  204                         writer.AddAttribute(HtmlTextWriterAttribute.Class, PageIndexBoxClass);
  205                     if (!Enabled || (PageCount <= 1 && AlwaysShow))
  206                         writer.AddAttribute(HtmlTextWriterAttribute.Disabled, "disabled");
  207                     writer.AddAttribute(HtmlTextWriterAttribute.Name, boxClientId);
  208                     writer.AddAttribute(HtmlTextWriterAttribute.Id, boxClientId);
  209                     string chkInputScript = "ANP_checkInput(\'" + boxClientId + "\'," + PageCount + ")";
  210                     string keydownScript = "ANP_keydown(event,\'" + UniqueID + "_btn\');";
  211                     string clickScript = "if(" + chkInputScript + "){ANP_goToPage(document.getElementById(\'" +
  212                                          boxClientId + "\'));};return false;";
  213 
  214                     writer.AddAttribute("onkeydown", keydownScript);
  215                     writer.RenderBeginTag(HtmlTextWriterTag.Input);
  216                     writer.RenderEndTag();
  217                     //Text after page index box
  218                     if (!string.IsNullOrEmpty(TextAfterPageIndexBox))
  219                         writer.Write(TextAfterPageIndexBox);
  220 
  221                     //button
  222                     if (!string.IsNullOrEmpty(SubmitButtonImageUrl))
  223                     {
  224                         writer.AddAttribute(HtmlTextWriterAttribute.Type, "image");
  225                         writer.AddAttribute(HtmlTextWriterAttribute.Src, SubmitButtonImageUrl);
  226                     }
  227                     else
  228                     {
  229                         writer.AddAttribute(HtmlTextWriterAttribute.Type, UrlPaging ? "button" : "submit");
  230                         writer.AddAttribute(HtmlTextWriterAttribute.Value, SubmitButtonText);
  231                     }
  232                     writer.AddAttribute(HtmlTextWriterAttribute.Name, UniqueID);
  233                     writer.AddAttribute(HtmlTextWriterAttribute.Id, UniqueID + "_btn");
  234                     if (!string.IsNullOrEmpty(SubmitButtonClass))
  235                         writer.AddAttribute(HtmlTextWriterAttribute.Class, SubmitButtonClass);
  236                     if (!string.IsNullOrEmpty(SubmitButtonStyle))
  237                         writer.AddAttribute(HtmlTextWriterAttribute.Style, SubmitButtonStyle);
  238                     if (!Enabled || (PageCount <= 1 && AlwaysShow))
  239                         writer.AddAttribute(HtmlTextWriterAttribute.Disabled, "disabled");
  240                     writer.AddAttribute(HtmlTextWriterAttribute.Onclick,
  241                                         (UrlPaging)
  242                                             ? clickScript
  243                                             : "if(" + chkInputScript + "){" +
  244                                               Page.ClientScript.GetPostBackEventReference(this, null) +
  245                                               "} else{return false}");
  246                     writer.RenderBeginTag(HtmlTextWriterTag.Input);
  247                     writer.RenderEndTag();
  248                 }
  249                 else //Dropdownlist
  250                 {
  251                     writer.AddAttribute(HtmlTextWriterAttribute.Name, boxClientId);
  252                     writer.AddAttribute(HtmlTextWriterAttribute.Id, boxClientId);
  253                     writer.AddAttribute(HtmlTextWriterAttribute.Onchange,
  254                                         UrlPaging
  255                                             ? "ANP_goToPage(this)"
  256                                             : Page.ClientScript.GetPostBackEventReference(this, null));
  257                     if (!string.IsNullOrEmpty(PageIndexBoxStyle))
  258                         writer.AddAttribute(HtmlTextWriterAttribute.Style, PageIndexBoxStyle);
  259                     if (!string.IsNullOrEmpty(PageIndexBoxClass))
  260                         writer.AddAttribute(HtmlTextWriterAttribute.Class, PageIndexBoxClass);
  261                     writer.RenderBeginTag(HtmlTextWriterTag.Select);
  262                     if (PageCount > 80) //list only part of page indices
  263                     {
  264                         if (CurrentPageIndex <= 15)
  265                         {
  266                             listPageIndices(writer, 1, 15);
  267                             addMoreListItem(writer, 16);
  268                             listPageIndices(writer, PageCount - 4, PageCount);
  269                         }
  270                         else if (CurrentPageIndex >= PageCount - 14)
  271                         {
  272                             listPageIndices(writer, 1, 5);
  273                             addMoreListItem(writer, PageCount - 15);
  274                             listPageIndices(writer, PageCount - 14, PageCount);
  275                         }
  276                         else
  277                         {
  278                             listPageIndices(writer, 1, 5);
  279                             addMoreListItem(writer, CurrentPageIndex - 6);
  280                             listPageIndices(writer, CurrentPageIndex - 5, CurrentPageIndex + 5);
  281                             addMoreListItem(writer, CurrentPageIndex + 6);
  282                             listPageIndices(writer, PageCount - 4, PageCount);
  283                         }
  284                     }
  285                     else //list all page indices
  286                         listPageIndices(writer, 1, PageCount);
  287                     writer.RenderEndTag();
  288                     if (!string.IsNullOrEmpty(TextAfterPageIndexBox))
  289                         writer.Write(TextAfterPageIndexBox);
  290                 }
  291             }
  292         }
  293 
  294         /// <summary>
  295         /// Get the navigation url for the paging button.
  296         /// </summary>
  297         /// <param name="pageIndex">the page index correspond to the button.</param>
  298         /// <returns>href string for the paging navigation button.</returns>
  299         private string GetHrefString(int pageIndex)
  300         {
  301             if (UrlPaging)
  302             {
  303                 int urlPageIndex = pageIndex;
  304                 string jsValue = "pi";
  305                 if (ReverseUrlPageIndex)
  306                 {
  307                     jsValue = "(" + PageCount + "-pi+1)";
  308                     urlPageIndex = pageIndex == -1 ? -1 : PageCount - pageIndex + 1;
  309                 }
  310                 if (EnableUrlRewriting)
  311                 {
  312                     Regex reg = new Regex("(?<p>%(?<m>[^%]+)%)", RegexOptions.Compiled | RegexOptions.IgnoreCase);
  313                     MatchCollection mts = reg.Matches(UrlRewritePattern);
  314                     string prmValue;
  315                     NameValueCollection urlParams = ConvertQueryStringToCollection(queryString);
  316                     string url = UrlRewritePattern;
  317                     foreach (Match m in mts)
  318                     {
  319                         prmValue = urlParams[m.Groups["m"].Value];
  320                         //if (prmValue != null)
  321                         url = url.Replace(m.Groups["p"].Value, prmValue);
  322                     }
  323                     return ResolveUrl(string.Format(url, (urlPageIndex == -1) ? "\"+" + jsValue + "+\"" : urlPageIndex.ToString()));
  324                 }
  325                 else
  326                 {
  327                     return BuildUrlString(UrlPageIndexName, (urlPageIndex == -1) ? "\"+" + jsValue + "+\"" : urlPageIndex.ToString());
  328                 }
  329             }
  330             return Page.ClientScript.GetPostBackClientHyperlink(this, pageIndex.ToString());
  331         }
  332 
  333         /// <summary>
  334         /// Replace the property placeholders in the CustomInfoHTML with the property values repectively
  335         /// </summary>
  336         /// <param name="origText">original CustomInfoHTML</param>
  337         /// <returns></returns>
  338         private string GetCustomInfoHtml(string origText)
  339         {
  340             if (!string.IsNullOrEmpty(origText) && origText.IndexOf('%') >= 0)
  341             {
  342                 string[] props = new string[] { "recordcount", "pagecount", "currentpageindex", "startrecordindex", "endrecordindex", "pagesize", "pagesremain", "recordsremain" };
  343                 StringBuilder sb = new StringBuilder(origText);
  344                 Regex reg = new Regex("(?<ph>%(?<pname>\\w{8,})%)", RegexOptions.Compiled | RegexOptions.IgnoreCase);
  345                 MatchCollection mts = reg.Matches(origText);
  346                 foreach (Match m in mts)
  347                 {
  348                     string p = m.Groups["pname"].Value.ToLower();
  349                     if (Array.IndexOf(props, p) >= 0)
  350                     {
  351                         string repValue = null;
  352                         switch (p)
  353                         {
  354                             case "recordcount":
  355                                 repValue = RecordCount.ToString(); break;
  356                             case "pagecount":
  357                                 repValue = PageCount.ToString(); break;
  358                             case "currentpageindex":
  359                                 repValue = CurrentPageIndex.ToString(); break;
  360                             case "startrecordindex":
  361                                 repValue = StartRecordIndex.ToString(); break;
  362                             case "endrecordindex":
  363                                 repValue = EndRecordIndex.ToString(); break;
  364                             case "pagesize":
  365                                 repValue = PageSize.ToString(); break;
  366                             case "pagesremain":
  367                                 repValue = PagesRemain.ToString(); break;
  368                             case "recordsremain":
  369                                 repValue = RecordsRemain.ToString(); break;
  370                         }
  371                         if (repValue != null)
  372                             sb.Replace(m.Groups["ph"].Value, repValue);
  373                     }
  374                 }
  375                 return sb.ToString();
  376             }
  377             return origText;
  378         }
  379 
  380         /// <summary>
  381         /// Convert raw query string to NameValueCollection
  382         /// </summary>
  383         /// <param name="s">raw query string</param>
  384         private static NameValueCollection ConvertQueryStringToCollection(string s)
  385         {
  386             NameValueCollection prms = new NameValueCollection();
  387             int num = (s != null) ? s.Length : 0;
  388             for (int i = 0; i < num; i++)
  389             {
  390                 int startIndex = i;
  391                 int num4 = -1;
  392                 while (i < num)
  393                 {
  394                     char ch = s[i];
  395                     if (ch == '=')
  396                     {
  397                         if (num4 < 0)
  398                         {
  399                             num4 = i;
  400                         }
  401                     }
  402                     else if (ch == '&')
  403                     {
  404                         break;
  405                     }
  406                     i++;
  407                 }
  408                 string skey = null;
  409                 string svalue;
  410                 if (num4 >= 0)
  411                 {
  412                     skey = s.Substring(startIndex, num4 - startIndex);
  413                     svalue = s.Substring(num4 + 1, (i - num4) - 1);
  414                 }
  415                 else
  416                 {
  417                     svalue = s.Substring(startIndex, i - startIndex);
  418                 }
  419                 prms.Add(skey, svalue);
  420                 if ((i == (num - 1)) && (s[i] == '&'))
  421                 {
  422                     prms.Add(null, string.Empty);
  423                 }
  424             }
  425             return prms;
  426         }
  427 
  428         /// <summary>
  429         /// add paging parameter and value to the current url or change parameter value if it already exists when using url paging mode
  430         /// </summary>
  431         /// <param name="sk">name of the url parameter to be added</param>
  432         /// <param name="sv">value of the url paramter to be added</param>
  433         /// <returns>href string for the navigattion button</returns>
  434         private string BuildUrlString(string sk, string sv)
  435         {
  436             StringBuilder ubuilder = new StringBuilder(80);
  437             bool keyFound = false;
  438             int num = (queryString != null) ? queryString.Length : 0;
  439             for (int i = 0; i < num; i++)
  440             {
  441                 int startIndex = i;
  442                 int num4 = -1;
  443                 while (i < num)
  444                 {
  445                     char ch = queryString[i];
  446                     if (ch == '=')
  447                     {
  448                         if (num4 < 0)
  449                         {
  450                             num4 = i;
  451                         }
  452                     }
  453                     else if (ch == '&')
  454                     {
  455                         break;
  456                     }
  457                     i++;
  458                 }
  459                 string skey = null;
  460                 string svalue;
  461                 if (num4 >= 0)
  462                 {
  463                     skey = queryString.Substring(startIndex, num4 - startIndex);
  464                     svalue = queryString.Substring(num4 + 1, (i - num4) - 1);
  465                 }
  466                 else
  467                 {
  468                     svalue = queryString.Substring(startIndex, i - startIndex);
  469                 }
  470                 ubuilder.Append(skey).Append("=");
  471                 if (skey == sk)
  472                 {
  473                     keyFound = true;
  474                     ubuilder.Append(sv);
  475                 }
  476                 else
  477                     ubuilder.Append(svalue);
  478                 ubuilder.Append("&");
  479             }
  480             if (!keyFound)
  481                 ubuilder.Append(sk).Append("=").Append(sv);
  482             ubuilder.Insert(0, "?").Insert(0, Path.GetFileName(currentUrl));
  483             return ubuilder.ToString().Trim('&');
  484         }
  485 
  486         /// <summary>
  487         /// Create first, prev, next or last button.
  488         /// </summary>
  489         /// <param name="writer">A <see cref="System.Web.UI.HtmlTextWriter"/> that represents the output stream to render HTML content on the client.</param>
  490         /// <param name="btn">the navigation button</param>
  491         private void CreateNavigationButton(HtmlTextWriter writer, NavigationButton btn)
  492         {
  493             if (!ShowFirstLast && (btn == NavigationButton.First || btn == NavigationButton.Last))
  494                 return;
  495             if (!ShowPrevNext && (btn == NavigationButton.Prev || btn == NavigationButton.Next))
  496                 return;
  497 
  498             if (PagingButtonLayoutType != PagingButtonLayoutType.None) //add css class and style attribute to pager item directly
  499             {
  500                 if (btn == NavigationButton.First || btn == NavigationButton.Last) //first page or last page button
  501                     AddClassAndStyle(FirstLastButtonsClass, FirstLastButtonsStyle, writer);
  502                 else
  503                     AddClassAndStyle(PrevNextButtonsClass, PrevNextButtonsStyle, writer);//next page or prevous page button
  504             }
  505             AddPagingButtonLayoutTag(writer); //<li> or <span>
  506 
  507             string linktext;
  508             bool disabled;
  509             int pageIndex;
  510             string btnname = btn.ToString().ToLower();
  511             bool isImgBtn = (//PagingButtonType == PagingButtonType.Image &&
  512                               NavigationButtonType == PagingButtonType.Image);
  513             if (btn == NavigationButton.First || btn == NavigationButton.Prev)
  514             {
  515                 disabled = (CurrentPageIndex <= 1) | !Enabled;
  516                 if (!ShowDisabledButtons && disabled)
  517                     return;
  518                 pageIndex = (btn == NavigationButton.First) ? 1 : (CurrentPageIndex - 1);
  519                 writeSpacingStyle(writer);
  520                 if (PagingButtonLayoutType == PagingButtonLayoutType.None) //add css class and style attribute to pager item directly
  521                 {
  522                     if (btn == NavigationButton.First || btn == NavigationButton.Last) //first page or last page button
  523                         AddClassAndStyle(FirstLastButtonsClass, FirstLastButtonsStyle, writer);
  524                     else
  525                         AddClassAndStyle(PrevNextButtonsClass, PrevNextButtonsStyle, writer);//next page or prevous page button
  526                 }
  527                 if (isImgBtn)
  528                 {
  529                     if (!disabled)
  530                     {
  531                         writer.AddAttribute(HtmlTextWriterAttribute.Href, GetHrefString(pageIndex));
  532                         AddToolTip(writer, pageIndex);
  533                         AddHyperlinkTarget(writer);
  534                         writer.RenderBeginTag(HtmlTextWriterTag.A);
  535                         writer.AddAttribute(HtmlTextWriterAttribute.Src,
  536                                             String.Concat(ImagePath, btnname, ButtonImageNameExtension,
  537                                                           ButtonImageExtension));
  538                         writer.AddAttribute(HtmlTextWriterAttribute.Border, "0");
  539                         if (ButtonImageAlign != ImageAlign.NotSet)
  540                             writer.AddAttribute(HtmlTextWriterAttribute.Align, ButtonImageAlign.ToString());
  541                         writer.RenderBeginTag(HtmlTextWriterTag.Img);
  542                         writer.RenderEndTag();
  543                         writer.RenderEndTag();
  544                     }
  545                     else
  546                     {
  547                         writer.AddAttribute(HtmlTextWriterAttribute.Src,
  548                                             String.Concat(ImagePath, btnname, DisabledButtonImageNameExtension,
  549                                                           ButtonImageExtension));
  550                         writer.AddAttribute(HtmlTextWriterAttribute.Border, "0");
  551                         if (ButtonImageAlign != ImageAlign.NotSet)
  552                             writer.AddAttribute(HtmlTextWriterAttribute.Align, ButtonImageAlign.ToString());
  553                         writer.RenderBeginTag(HtmlTextWriterTag.Img);
  554                         writer.RenderEndTag();
  555                     }
  556                 }
  557                 else
  558                 {
  559                     linktext = (btn == NavigationButton.Prev) ? PrevPageText : FirstPageText;
  560                     if (disabled)
  561                         writer.AddAttribute(HtmlTextWriterAttribute.Disabled, "disabled");
  562                     else
  563                     {
  564                         //WriteCssClass(writer);
  565                         AddToolTip(writer, pageIndex);
  566                         AddHyperlinkTarget(writer);
  567                         writer.AddAttribute(HtmlTextWriterAttribute.Href, GetHrefString(pageIndex));
  568                     }
  569                     writer.RenderBeginTag(HtmlTextWriterTag.A);
  570                     writer.Write(linktext);
  571                     writer.RenderEndTag();
  572                 }
  573             }
  574             else
  575             {
  576                 disabled = (CurrentPageIndex >= PageCount) | !Enabled;
  577                 if (!ShowDisabledButtons && disabled)
  578                     return;
  579                 pageIndex = (btn == NavigationButton.Last) ? PageCount : (CurrentPageIndex + 1);
  580                 writeSpacingStyle(writer);
  581                 if (PagingButtonLayoutType == PagingButtonLayoutType.None) //add css class and style attribute to pager item directly
  582                 {
  583                     if (btn == NavigationButton.First || btn == NavigationButton.Last) //first page or last page button
  584                         AddClassAndStyle(FirstLastButtonsClass, FirstLastButtonsStyle, writer);
  585                     else
  586                         AddClassAndStyle(PrevNextButtonsClass, PrevNextButtonsStyle, writer);//next page or prevous page button
  587                 }
  588                 if (isImgBtn)
  589                 {
  590                     if (!disabled)
  591                     {
  592                         writer.AddAttribute(HtmlTextWriterAttribute.Href, GetHrefString(pageIndex));
  593                         AddToolTip(writer, pageIndex);
  594                         AddHyperlinkTarget(writer);
  595                         writer.RenderBeginTag(HtmlTextWriterTag.A);
  596                         writer.AddAttribute(HtmlTextWriterAttribute.Src,
  597                                             String.Concat(ImagePath, btnname, ButtonImageNameExtension,
  598                                                           ButtonImageExtension));
  599                         writer.AddAttribute(HtmlTextWriterAttribute.Border, "0");
  600                         if (ButtonImageAlign != ImageAlign.NotSet)
  601                             writer.AddAttribute(HtmlTextWriterAttribute.Align, ButtonImageAlign.ToString());
  602                         writer.RenderBeginTag(HtmlTextWriterTag.Img);
  603                         writer.RenderEndTag();
  604                         writer.RenderEndTag();
  605                     }
  606                     else
  607                     {
  608                         writer.AddAttribute(HtmlTextWriterAttribute.Src,
  609                                             String.Concat(ImagePath, btnname, DisabledButtonImageNameExtension,
  610                                                           ButtonImageExtension));
  611                         writer.AddAttribute(HtmlTextWriterAttribute.Border, "0");
  612                         if (ButtonImageAlign != ImageAlign.NotSet)
  613                             writer.AddAttribute(HtmlTextWriterAttribute.Align, ButtonImageAlign.ToString());
  614                         writer.RenderBeginTag(HtmlTextWriterTag.Img);
  615                         writer.RenderEndTag();
  616                     }
  617                 }
  618                 else
  619                 {
  620                     linktext = (btn == NavigationButton.Next) ? NextPageText : LastPageText;
  621                     if (disabled)
  622                         writer.AddAttribute(HtmlTextWriterAttribute.Disabled, "disabled");
  623                     else
  624                     {
  625                         //WriteCssClass(writer);
  626                         AddToolTip(writer, pageIndex);
  627                         writer.AddAttribute(HtmlTextWriterAttribute.Href, GetHrefString(pageIndex));
  628                         AddHyperlinkTarget(writer);
  629                     }
  630                     writer.RenderBeginTag(HtmlTextWriterTag.A);
  631                     writer.Write(linktext);
  632                     writer.RenderEndTag();
  633                 }
  634             }
  635             if (PagingButtonLayoutType != PagingButtonLayoutType.None)
  636                 writer.RenderEndTag(); //</li> or </span>
  637         }
  638 
  639         /// <summary>
  640         /// Write css class name.
  641         /// </summary>
  642         /// <param name="writer">A <see cref="System.Web.UI.HtmlTextWriter"/> that represents the output stream to render HTML content on the client. </param>
  643         //private void WriteCssClass(HtmlTextWriter writer)
  644         //{
  645         //    if (cssClassName != null && cssClassName.Trim().Length > 0)
  646         //        writer.AddAttribute(HtmlTextWriterAttribute.Class, cssClassName);
  647         //}
  648 
  649         /// <summary>
  650         /// Add tool tip text to navigation button.
  651         /// </summary>
  652         private void AddToolTip(HtmlTextWriter writer, int pageIndex)
  653         {
  654             if (ShowNavigationToolTip)
  655             {
  656                 writer.AddAttribute(HtmlTextWriterAttribute.Title, String.Format(NavigationToolTipTextFormatString, pageIndex));
  657             }
  658         }
  659         /// <summary>
  660         /// add paging button layout tag
  661         /// </summary>
  662         private void AddPagingButtonLayoutTag(HtmlTextWriter writer)
  663         {
  664             if (PagingButtonLayoutType == PagingButtonLayoutType.UnorderedList)
  665                 writer.RenderBeginTag(HtmlTextWriterTag.Li); //<li>
  666             else if (PagingButtonLayoutType == PagingButtonLayoutType.Span)
  667                 writer.RenderBeginTag(HtmlTextWriterTag.Span); //<span>
  668         }
  669 
  670         /// <summary>
  671         /// Create numeric paging button.
  672         /// </summary>
  673         /// <param name="writer">A <see cref="System.Web.UI.HtmlTextWriter"/> that represents the output stream to render HTML content on the client.</param>
  674         /// <param name="index">the page index correspond to the paging button</param>
  675         private void CreateNumericButton(HtmlTextWriter writer, int index)
  676         {
  677             bool isCurrent = (index == CurrentPageIndex);
  678 
  679             if ((!isCurrent && PagingButtonLayoutType != PagingButtonLayoutType.None) || (isCurrent && PagingButtonLayoutType == PagingButtonLayoutType.UnorderedList)) //current page button already wrapped in span
  680             {
  681                 if (!isCurrent) //exclude current page index button
  682                     AddClassAndStyle(PagingButtonsClass, PagingButtonsStyle, writer);
  683                 AddPagingButtonLayoutTag(writer); //<li>
  684             }
  685 
  686             if (/*PagingButtonType == PagingButtonType.Image && */NumericButtonType == PagingButtonType.Image)
  687             {
  688                 writeSpacingStyle(writer);
  689                 if (!isCurrent)
  690                 {
  691                     if (Enabled)
  692                         writer.AddAttribute(HtmlTextWriterAttribute.Href, GetHrefString(index));
  693                     //if (PagingButtonLayoutType == PagingButtonLayoutType.None) //add css class and style attribute to hyper link directly
  694                         AddClassAndStyle(PagingButtonsClass, PagingButtonsStyle, writer);
  695 
  696                     AddToolTip(writer, index);
  697                     AddHyperlinkTarget(writer);
  698                     writer.RenderBeginTag(HtmlTextWriterTag.A);
  699                     CreateNumericImages(writer, index, false);
  700                     writer.RenderEndTag();
  701                 }
  702                 else
  703                 {
  704                     if (!string.IsNullOrEmpty(CurrentPageButtonClass))
  705                         writer.AddAttribute(HtmlTextWriterAttribute.Class, CurrentPageButtonClass);
  706                     if (!string.IsNullOrEmpty(CurrentPageButtonStyle))
  707                         writer.AddAttribute(HtmlTextWriterAttribute.Style, CurrentPageButtonStyle);
  708                     writer.RenderBeginTag(HtmlTextWriterTag.Span);
  709                     CreateNumericImages(writer, index, true);
  710                     writer.RenderEndTag();
  711                 }
  712             }
  713             else
  714             {
  715                 writeSpacingStyle(writer);
  716                 if (isCurrent)
  717                 {
  718                     if (string.IsNullOrEmpty(CurrentPageButtonClass) && string.IsNullOrEmpty(CurrentPageButtonStyle))
  719                     {
  720                         writer.AddStyleAttribute(HtmlTextWriterStyle.FontWeight, "Bold");
  721                         writer.AddStyleAttribute(HtmlTextWriterStyle.Color, "red");
  722                     }
  723                     else
  724                     {
  725                         if (!string.IsNullOrEmpty(CurrentPageButtonClass))
  726                             writer.AddAttribute(HtmlTextWriterAttribute.Class, CurrentPageButtonClass);
  727                         if (!string.IsNullOrEmpty(CurrentPageButtonStyle))
  728                             writer.AddAttribute(HtmlTextWriterAttribute.Style, CurrentPageButtonStyle);
  729                     }
  730                     writer.RenderBeginTag(HtmlTextWriterTag.Span);
  731                     if (!string.IsNullOrEmpty(CurrentPageButtonTextFormatString))
  732                         writer.Write(String.Format(CurrentPageButtonTextFormatString, index));
  733                     else
  734                         writer.Write(index);
  735                     writer.RenderEndTag();
  736                 }
  737                 else
  738                 {
  739                     if (Enabled)
  740                     {
  741                         writer.AddAttribute(HtmlTextWriterAttribute.Href, GetHrefString(index));
  742                         AddClassAndStyle(PagingButtonsClass, PagingButtonsStyle, writer); //add css class and style
  743                     }
  744                     //WriteCssClass(writer);
  745                     AddToolTip(writer, index);
  746                     AddHyperlinkTarget(writer);
  747                     writer.RenderBeginTag(HtmlTextWriterTag.A);
  748                     if (!string.IsNullOrEmpty(NumericButtonTextFormatString))
  749                         writer.Write(String.Format(NumericButtonTextFormatString, index));
  750                     else
  751                         writer.Write(index);
  752                     writer.RenderEndTag();
  753                 }
  754             }
  755             if ((!isCurrent && PagingButtonLayoutType != PagingButtonLayoutType.None) || (isCurrent && PagingButtonLayoutType == PagingButtonLayoutType.UnorderedList))
  756                 writer.RenderEndTag(); //</li>
  757         }
  758 
  759         /// <summary>
  760         /// Create numeric image button.
  761         /// </summary>
  762         /// <param name="writer">A <see cref="System.Web.UI.HtmlTextWriter"/> that represents the output stream to render HTML content on the client.</param>
  763         /// <param name="index">the page index correspond to the button.</param>
  764         /// <param name="isCurrent">if the page index correspond to the button is the current page index</param>
  765         private void CreateNumericImages(HtmlTextWriter writer, int index, bool isCurrent)
  766         {
  767             AddPagingButtonLayoutTag(writer); //<li> or <span>
  768 
  769             string indexStr = index.ToString();
  770             for (int i = 0; i < indexStr.Length; i++)
  771             {
  772                 writer.AddAttribute(HtmlTextWriterAttribute.Src, String.Concat(ImagePath, indexStr[i], (isCurrent) ? CpiButtonImageNameExtension : ButtonImageNameExtension, ButtonImageExtension));
  773                 if (ButtonImageAlign != ImageAlign.NotSet)
  774                     writer.AddAttribute(HtmlTextWriterAttribute.Align, ButtonImageAlign.ToString());
  775                 writer.AddAttribute(HtmlTextWriterAttribute.Border, "0");
  776                 writer.RenderBeginTag(HtmlTextWriterTag.Img);
  777                 writer.RenderEndTag();
  778             }
  779             if (PagingButtonLayoutType != PagingButtonLayoutType.None)
  780                 writer.RenderEndTag(); //</li> or </span>
  781         }
  782 
  783         /// <summary>
  784         /// create more (...) button.
  785         /// </summary>
  786         private void CreateMoreButton(HtmlTextWriter writer, int pageIndex)
  787         {
  788             AddClassAndStyle(MoreButtonsClass, MoreButtonsStyle, writer);
  789             AddPagingButtonLayoutTag(writer); //<li> or <span>
  790 
  791             writeSpacingStyle(writer);
  792             if (Enabled)
  793             {
  794                 writer.AddAttribute(HtmlTextWriterAttribute.Href, GetHrefString(pageIndex));
  795                 AddToolTip(writer, pageIndex);
  796                 AddHyperlinkTarget(writer);
  797             }
  798             writer.RenderBeginTag(HtmlTextWriterTag.A); //<a>
  799             if ( /*PagingButtonType == PagingButtonType.Image && */MoreButtonType == PagingButtonType.Image)
  800             {
  801                 writer.AddAttribute(HtmlTextWriterAttribute.Src,
  802                                     String.Concat(ImagePath, "more", ButtonImageNameExtension, ButtonImageExtension));
  803                 writer.AddAttribute(HtmlTextWriterAttribute.Border, "0");
  804                 if (ButtonImageAlign != ImageAlign.NotSet)
  805                     writer.AddAttribute(HtmlTextWriterAttribute.Align, ButtonImageAlign.ToString());
  806                 writer.RenderBeginTag(HtmlTextWriterTag.Img); //<img>
  807                 writer.RenderEndTag(); //</img>
  808             }
  809             else
  810                 writer.Write("...");
  811 
  812             writer.RenderEndTag(); //</a>
  813 
  814             if (PagingButtonLayoutType != PagingButtonLayoutType.None)
  815                 writer.RenderEndTag(); //</li> or </span>
  816         }
  817 
  818         /// <summary>
  819         /// Add paging button spacing styles to HtmlTextWriter
  820         /// </summary>
  821         private void writeSpacingStyle(HtmlTextWriter writer)
  822         {
  823             if (PagingButtonSpacing.Value != 0)
  824                 writer.AddStyleAttribute(HtmlTextWriterStyle.MarginRight, PagingButtonSpacing.ToString());
  825         }
  826 
  827         /// <summary>
  828         /// add target attribute to paging hyperlink
  829         /// </summary>
  830         private void AddHyperlinkTarget(HtmlTextWriter writer)
  831         {
  832             if (!string.IsNullOrEmpty(UrlPagingTarget))
  833                 writer.AddAttribute(HtmlTextWriterAttribute.Target, UrlPagingTarget);
  834         }
  835 
  836         /// <summary>
  837         /// add css class and style attribute to HtmlTextWriter
  838         /// </summary>
  839         private void AddClassAndStyle(string clsname, string style, HtmlTextWriter writer)
  840         {
  841             if (!string.IsNullOrEmpty(clsname))
  842                 writer.AddAttribute(HtmlTextWriterAttribute.Class, clsname);
  843             if (!string.IsNullOrEmpty(style))
  844                 writer.AddAttribute(HtmlTextWriterAttribute.Style, style);
  845         }
  846 
  847         #endregion
  848     }
  849 }