private MvcHtmlString CreateWrappedPagerElement(PagerItem item, string el) { string navStr = el; string s = string.Empty; if (!string.IsNullOrEmpty(navStr)) { if (item.Disabled) { s = " disabled"; } switch (item.Type) { case PagerItemType.FirstPage: s += " prev"; break; case PagerItemType.LastPage: s += " next"; navStr = navStr + _pagerOptions.SeparatorHtml; break; case PagerItemType.NextPage: s += " next"; navStr = _pagerOptions.SeparatorHtml + navStr; break; case PagerItemType.PrevPage: if ((!string.IsNullOrEmpty(_pagerOptions.NavigationPagerItemWrapperFormatString) || !string.IsNullOrEmpty(_pagerOptions.PagerItemWrapperFormatString))) { navStr = string.Format( _pagerOptions.NavigationPagerItemWrapperFormatString ?? _pagerOptions.PagerItemWrapperFormatString, el); } else { s += " prev"; } navStr = navStr + _pagerOptions.SeparatorHtml; break; case PagerItemType.MorePage: if ((!string.IsNullOrEmpty(_pagerOptions.MorePagerItemWrapperFormatString) || !string.IsNullOrEmpty(_pagerOptions.PagerItemWrapperFormatString))) { navStr = string.Format( _pagerOptions.MorePagerItemWrapperFormatString ?? _pagerOptions.PagerItemWrapperFormatString, el); } break; case PagerItemType.NumericPage: if (item.PageIndex == _pageIndex) //current page { s += " active"; } break; } } navStr = "<li class=\"" + s + "\">" + navStr + "</li>"; return(MvcHtmlString.Create(navStr)); }
/// <summary> /// The render pager. /// </summary> /// <returns> /// The <see cref="MvcHtmlString"/>. /// </returns> internal MvcHtmlString RenderPager() { if (this._totalPageCount <= 1 && this._pagerOptions.AutoHide) { return(MvcHtmlString.Create("\r\n<!--MvcPager v2.0 for ASP.NET MVC 4.0+ © 2009-2013 Webdiyer (http://www.webdiyer.com)-->\r\n")); } if ((this._pageIndex > this._totalPageCount && this._totalPageCount > 0) || this._pageIndex < 1) { return(MvcHtmlString.Create(string.Format("{0}<div style=\"color:red;font-weight:bold\">{1}</div>{0}", "\r\n<!--MvcPager v2.0 for ASP.NET MVC 4.0+ © 2009-2013 Webdiyer (http://www.webdiyer.com)-->\r\n", this._pagerOptions.PageIndexOutOfRangeErrorMessage))); } List <PagerItem> pagerItems = new List <PagerItem>(); if (this._pagerOptions.ShowFirstLast) { this.AddFirst(pagerItems); } if (this._pagerOptions.ShowPrevNext) { this.AddPrevious(pagerItems); } if (this._pagerOptions.ShowNumericPagerItems) { if (this._pagerOptions.AlwaysShowFirstLastPageNumber && this._startPageIndex > 1) { pagerItems.Add(new PagerItem("1", 1, false, PagerItemType.NumericPage)); } if (this._pagerOptions.ShowMorePagerItems && ((!this._pagerOptions.AlwaysShowFirstLastPageNumber && this._startPageIndex > 1) || (this._pagerOptions.AlwaysShowFirstLastPageNumber && this._startPageIndex > 2))) { this.AddMoreBefore(pagerItems); } this.AddPageNumbers(pagerItems); if (this._pagerOptions.ShowMorePagerItems && ((!this._pagerOptions.AlwaysShowFirstLastPageNumber && this._endPageIndex < this._totalPageCount) || (this._pagerOptions.AlwaysShowFirstLastPageNumber && this._totalPageCount > this._endPageIndex + 1))) { this.AddMoreAfter(pagerItems); } if (this._pagerOptions.AlwaysShowFirstLastPageNumber && this._endPageIndex < this._totalPageCount) { pagerItems.Add(new PagerItem(this._totalPageCount.ToString(CultureInfo.InvariantCulture), this._totalPageCount, false, PagerItemType.NumericPage)); } } if (this._pagerOptions.ShowPrevNext) { this.AddNext(pagerItems); } if (this._pagerOptions.ShowFirstLast) { this.AddLast(pagerItems); } StringBuilder sb = new StringBuilder(); if (this._ajaxPagingEnabled) { using (List <PagerItem> .Enumerator enumerator = pagerItems.GetEnumerator()) { while (enumerator.MoveNext()) { PagerItem item = enumerator.Current; sb.Append(this.GenerateAjaxPagerElement(item)); } goto IL_245; } } foreach (PagerItem item2 in pagerItems) { sb.Append(this.GeneratePagerElement(item2)); } IL_245: TagBuilder tb = new TagBuilder(this._pagerOptions.ContainerTagName); if (!string.IsNullOrEmpty(this._pagerOptions.Id)) { tb.GenerateId(this._pagerOptions.Id); } if (!string.IsNullOrEmpty(this._pagerOptions.CssClass)) { tb.AddCssClass(this._pagerOptions.CssClass); } if (!string.IsNullOrEmpty(this._pagerOptions.HorizontalAlign)) { string strAlign = "text-align:" + this._pagerOptions.HorizontalAlign.ToLower(); if (this._htmlAttributes == null) { this._htmlAttributes = new RouteValueDictionary { { "style", strAlign } }; } else { if (this._htmlAttributes.Keys.Contains("style")) { IDictionary <string, object> htmlAttributes; (htmlAttributes = this._htmlAttributes)["style"] = htmlAttributes["style"] + ";" + strAlign; } } } tb.MergeAttributes <string, object>(this._htmlAttributes, true); if (this._ajaxPagingEnabled) { IDictionary <string, object> attrs = this._ajaxOptions.ToUnobtrusiveHtmlAttributes(); attrs.Remove("data-ajax-url"); attrs.Remove("data-ajax-mode"); if (this._ajaxOptions.EnablePartialLoading) { attrs.Add("data-ajax-partialloading", "true"); } if (this._pageIndex > 1) { attrs.Add("data-ajax-currentpage", this._pageIndex); } if (!string.IsNullOrWhiteSpace(this._ajaxOptions.DataFormId)) { attrs.Add("data-ajax-dataformid", "#" + this._ajaxOptions.DataFormId); } this.AddDataAttributes(attrs); tb.MergeAttributes <string, object>(attrs, true); } if (this._pagerOptions.ShowPageIndexBox) { if (!this._ajaxPagingEnabled) { Dictionary <string, object> attrs2 = new Dictionary <string, object>(); this.AddDataAttributes(attrs2); tb.MergeAttributes <string, object>(attrs2, true); } sb.Append(this.BuildGoToPageSection()); } else { sb.Length -= this._pagerOptions.PagerItemsSeperator.Length; } tb.InnerHtml = sb.ToString(); return(MvcHtmlString.Create("\r\n<!--MvcPager v2.0 for ASP.NET MVC 4.0+ © 2009-2013 Webdiyer (http://www.webdiyer.com)-->\r\n" + tb.ToString(TagRenderMode.Normal) + "\r\n<!--MvcPager v2.0 for ASP.NET MVC 4.0+ © 2009-2013 Webdiyer (http://www.webdiyer.com)-->\r\n")); }
private string GenerateAnchor(PagerItem item) { if (_msAjaxPaging) { var routeValues = GetCurrentRouteValues(_ajax.ViewContext); if (item.PageIndex == 0) { routeValues[_pagerOptions.PageIndexParameterName] = ScriptPageIndexName; } else { routeValues[_pagerOptions.PageIndexParameterName] = item.PageIndex; } if (!string.IsNullOrEmpty(_routeName)) { return(_ajax.RouteLink(item.Text, _routeName, routeValues, _ajaxOptions).ToString()); } return(_ajax.RouteLink(item.Text, routeValues, _ajaxOptions).ToString()); } string url = GenerateUrl(item.PageIndex); if (_pagerOptions.UseJqueryAjax) { StringBuilder scriptBuilder = new StringBuilder(); //ignore OnSuccess property if (!string.IsNullOrEmpty(_ajaxOptions.OnFailure) || !string.IsNullOrEmpty(_ajaxOptions.OnBegin) || (!string.IsNullOrEmpty(_ajaxOptions.OnComplete) && _ajaxOptions.HttpMethod.ToUpper() != "GET")) { scriptBuilder.Append("$.ajax({type:\'").Append(_ajaxOptions.HttpMethod.ToUpper() == "GET" ? "get" : "post"); scriptBuilder.Append("\',url:$(this).attr(\'href\'),success:function(data,status,xhr){$(\'#"); scriptBuilder.Append(_ajaxOptions.UpdateTargetId).Append("\').html(data);}"); if (!string.IsNullOrEmpty(_ajaxOptions.OnFailure)) { scriptBuilder.Append(",error:").Append(HttpUtility.HtmlAttributeEncode(_ajaxOptions.OnFailure)); } if (!string.IsNullOrEmpty(_ajaxOptions.OnBegin)) { scriptBuilder.Append(",beforeSend:").Append(HttpUtility.HtmlAttributeEncode(_ajaxOptions.OnBegin)); } if (!string.IsNullOrEmpty(_ajaxOptions.OnComplete)) { scriptBuilder.Append(",complete:").Append( HttpUtility.HtmlAttributeEncode(_ajaxOptions.OnComplete)); } scriptBuilder.Append("});return false;"); } else { if (_ajaxOptions.HttpMethod.ToUpper() == "GET") { scriptBuilder.Append("$(\'#").Append(_ajaxOptions.UpdateTargetId); scriptBuilder.Append("\').load($(this).attr(\'href\')"); if (!string.IsNullOrEmpty(_ajaxOptions.OnComplete)) { scriptBuilder.Append(",").Append(HttpUtility.HtmlAttributeEncode(_ajaxOptions.OnComplete)); } scriptBuilder.Append(");return false;"); } else { scriptBuilder.Append("$.post($(this).attr(\'href\'),$('#sbmitform').serialize(), function(data) {$(\'#"); scriptBuilder.Append(_ajaxOptions.UpdateTargetId); scriptBuilder.Append("\').html(data); Load();});return false;"); } } string htmlstr = "<a href=\"{0}\" onclick=\"{1}\">{2}</a>"; if (PagerItemType.NumericPage != item.Type) { htmlstr = "<a class=\"next_page\" href=\"{0}\" onclick=\"{1}\">{2}</a>"; } return(string.IsNullOrEmpty(url) ? _html.Encode(item.Text) : String.Format(CultureInfo.InvariantCulture, htmlstr, url, scriptBuilder, item.Text)); } return("<a href=\"" + url + "\" onclick=\"window.open(this.attributes.getNamedItem('href').value,'_self')\"></a>"); }