当前位置:Gxlcms > html代码 > 自动化分页,HTML代码控制思想_html/css_WEB-ITnose

自动化分页,HTML代码控制思想_html/css_WEB-ITnose

时间:2021-07-01 10:21:17 帮助过:18人阅读

1、绑定分页数据

  Request:主要是获取当前查询中所参与的所有条件

  pageSize:分页显示数量

  totalCount:分页总数

  currentPageStr:自定义参数(后面会更新该参数的用法和途径)

  tab:自定义参数(后面会更新该参数的用法和途径,和上面currentPageStr配合使用,需优化)

  pageIndexNumber:页面上显示当前页前、后多少页数数的数字

      如:显示当前页前后个5页的数字

  IsShouCount:是否显示总数量

2、用法,Response.Write页面输出一段html

            <%Response.Write(ZCJExtensions.Pager(Request, 40, iCount, "pi",IsShouCount:true));%>            

3、源代码 方法主要用到请求头类的使用,HttpRequest

public static class ZCJExtensions{    ///     /// 分页Pager显示(分页请求字符串默认为pi)    ///     /// 每页记录的数量    /// 总记录数    /// 显示页索引的数量    ///     public static string Pager(int pageSize, int totalCount,int pageIndexNumber = 10)    {        return ZCJExtensions.Pager(HttpContext.Current.Request, pageSize, totalCount, "pi", "", pageIndexNumber);    }    ///     /// 分页Pager显示        ///     /// 请求    /// 每页记录的数量    /// 总记录数    /// 请求字符串    /// 切换ID    /// 显示页索引的数量    ///     public static string Pager(HttpRequest requ, int pageSize, int totalCount, string currentPageStr = "1",string tab="",int pageIndexNumber = 10,bool IsShouCount=false)    {        //var queryString = html.ViewContext.HttpContext.Request.QueryString;        string html = "";        var queryString = requ.QueryString;        int currentPage = 1; //当前页            var totalPages = Math.Max((totalCount + pageSize - 1) / pageSize, 1); //总页数            //var dict = new System.Web.Routing.RouteValueDictionary(html.ViewContext.RouteData.Values);        var dict = new System.Web.Routing.RouteValueDictionary(requ.Form);        var output = new System.Text.StringBuilder();        var url = requ.Url.AbsolutePath + "?" + currentPageStr + "=(*)";        if (tab.Length > 0)        {            url += "&tab=" + tab;        }        //与相应的QueryString绑定           foreach (string key in queryString.Keys)        {            if (!string.IsNullOrEmpty(key) && queryString[key] != null )            {                dict[key] = queryString[key];                if (key != currentPageStr && key.ToLower()!="tab")                {                    url += "&" + key + "=" + queryString[key];                }            }        }        //获取 ~/Page/{page number} 的页号参数          int.TryParse((dict[currentPageStr] != null) ? dict[currentPageStr].ToString() : "0", out currentPage);        if (currentPage <= 0) currentPage = 1;        if (totalPages > 1)        {            if (currentPage != 1)            {                dict[currentPageStr] = 1;                //output.AppendFormat("{0} ", html.RouteLink("<<", dict));                var pageunm = "1";                var tempurl = url.Replace("(*)", pageunm);                output.AppendFormat("{0}", "首页");            }            if (currentPage > 1)            {                //处理上一页的连接                    dict[currentPageStr] = currentPage - 1;                var pageunm = (currentPage - 1).ToString();                var tempurl = url.Replace("(*)", pageunm);                //output.Append(html.RouteLink("<", dict));                output.Append("上一页");            }            else            {                //output.Append("<");            }            output.Append("");            int currint = pageIndexNumber / 2;            for (int i = 0; i <= pageIndexNumber; i++)            {                //一共最多显示10个页码,前面5个,后面5个                    if ((currentPage + i - currint) >= 1 && (currentPage + i - currint) <= totalPages)                    if (currint == i)                    {                        //当前页处理                            output.Append(string.Format("{0}", currentPage));                    }                    else                    {                        //一般页处理                           dict[currentPageStr] = currentPage + i - currint;                        var pageunm = (currentPage + i - currint).ToString();                        var tempurl = url.Replace("(*)", pageunm);                        //output.Append(html.RouteLink((currentPage + i - currint).ToString(), dict));                        output.Append("" + pageunm + "");                    }                output.Append("");            }            if (currentPage < totalPages)            {                //处理下一页的链接                   dict[currentPageStr] = currentPage + 1;                var pageunm = (currentPage + 1).ToString();                //output.Append(html.RouteLink(">", dict));                var tempurl = url.Replace("(*)", pageunm);                output.Append("下一页");            }            else            {                //output.Append(">");            }            output.Append("");            if (currentPage != totalPages)            {                dict[currentPageStr] = totalPages;                var pageunm = totalPages.ToString();                var tempurl = url.Replace("(*)", pageunm);                //output.Append(html.RouteLink(">>", dict));                output.Append("尾页");            }            output.Append(" ");        }        output.AppendFormat("第{0}页/共{1}页", currentPage, totalPages);//这个统计加不加都行           if (IsShouCount == true)         {            output.AppendFormat(",共{0}条记录", totalCount);        }        //HtmlString        return output.ToString();    }}

人气教程排行