コード例 #1
0
        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)
            {
                if (_html.ViewContext.UnobtrusiveJavaScriptEnabled)
                {
                    var tag = new TagBuilder("a")
                    {
                        InnerHtml = item.Text
                    };
                    tag.MergeAttribute("href", url);
                    // 添加页码在每个a上   add by Liuyu 2012/03/27
                    tag.MergeAttribute("p", "" + item.PageIndex);
                    tag.MergeAttributes(_ajaxOptions.ToUnobtrusiveHtmlAttributes());
                    return(string.IsNullOrEmpty(url)
                        ? _html.Encode(item.Text) : tag.ToString(TagRenderMode.Normal));
                }
                var scriptBuilder = new StringBuilder();

                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\'), function(data) {$(\'#");
                        scriptBuilder.Append(_ajaxOptions.UpdateTargetId);
                        scriptBuilder.Append("\').html(data);});return false;");
                    }
                }
                return(string.IsNullOrEmpty(url)
                           ? _html.Encode(item.Text)
                           : String.Format(CultureInfo.InvariantCulture,
                                           "<a href=\"{0}\" onclick=\"{1}\">{2}</a>",
                                           url, scriptBuilder, item.Text));
            }
            return("<a href=\"" + url +
                   "\" onclick=\"window.open(this.attributes.getNamedItem('href').value,'_self')\"></a>");
        }