这两天自己模仿写的一个Asp.Net的显示分页方法 附加实体转换和存储过程

8/10/2015来源:ASP.NET技巧人气:1423

这两天自己模仿写的一个asp.net的显示分页方法 附加实体转换和存储过程

之前自己一直用Aspnetpager控件来显示项目中的分页,但是每次都要拖一个aspnetpager的控件进去,感觉很不舒服,因为现在自己写的webform都不用服务器控件了,所以自己仿照aspnetpager写了一个精简实用的返回分页显示的html方法,其他话不说了,直接上代码。分页显示信息的实体类: View Codepublic class Pager { PRivate string _firstPageText; /// <summary> /// 最前一页文字显示 默认显示为"首页" /// </summary> public string FirstPageText { get { return string.IsNullOrEmpty(_firstPageText)? "首页" : _firstPageText; } set { _firstPageText = value; } }

private string _prePageText; /// <summary> /// 上一页文字显示 默认显示为"上一页" /// </summary> public string PrePageText { get { return string.IsNullOrEmpty(_prePageText) ? "上一页" : _prePageText; } set { _prePageText = value; } }

private string _nextPageText; /// <summary> /// 下一页文字显示 默认显示为"下一页" /// </summary> public string NextPageText { get { return string.IsNullOrEmpty(_nextPageText) ? "下一页" : _nextPageText; } set { _nextPageText = value; } }

private string _lastPageText; /// <summary> /// 末页文字显示 默认显示为"末页" /// </summary> public string LastPageText { get { return string.IsNullOrEmpty(_lastPageText) ? "末页" : _lastPageText; } set { _lastPageText = value; } }

/// <summary> /// 总记录数 /// </summary> public int RecordCount { get; set; }

private int _pagesize=15; /// <summary> /// 每页分页尺寸 默认为15 /// </summary> public int PageSize { get { return _pagesize == 0 ? 15 : _pagesize; }set{ _pagesize = value; } }

private int _pageIndex=1; /// <summary> /// 当前页码 /// </summary> public int PageIndex { get { return _pageIndex == 0 ? 1 : _pageIndex; } set { _pageIndex = value; } }

private int _maxShowPageSize = 10; /// <summary> /// 显示页码列表的最大个数 默认为10 /// </summary> public int MaxShowPageSize { get { return _maxShowPageSize; } set { _maxShowPageSize = value; } }

private string _queryStringName; /// <summary> /// 页码在浏览器中传值的名称 默认为page /// </summary> public string QueryStringName { get { return string.IsNullOrEmpty(_queryStringName)? "page" : _queryStringName; } set { _queryStringName = value; } }

/// <summary> /// 页面的URL /// </summary> public string URL { get { string url = HttpContext.Current.Request.Url.AbsoluteUri;//当前页面绝对路径 if (EnableUrlRewriting)//使用url重写 { url=url.Substring(0, url.LastIndexOf("/") + 1);//获取当前页面的目录路径 url += UrlRewritePattern; } else {//普通带问号的页面传值 //demo.aspx //demo.aspx?a=1 //demo.aspx?page=1 //demo.aspx?a=2&page=1 if (url.Contains("aspx?")) { if (Regex.IsMatch(url,@"page=[0-9]*$",RegexOptions.IgnoreCase))//如果存在page=*的字符串 { url = Regex.Replace(url, @"page=[0-9]*$", "", RegexOptions.IgnoreCase);//替换掉page=*的字符串 } url += QueryStringName + "={0}"; } else { url += "?" + QueryStringName + "={0}"; } } return url; } }

private bool _enableUrlRewriting; /// <summary> /// URL是否重写 默认为flase /// </summary> public bool EnableUrlRewriting { get { return (object)_enableUrlRewriting == null ? false : _enableUrlRewriting; } set { _enableUrlRewriting = value; } }

/// <summary> /// 页面URL重写规则,将页码传值用{0}来代替 比如list-1-{0}.html,启用该功能需要将EnableUrlRewriting属性设为true /// </summary> public string UrlRewritePattern { get; set; }

private string _className; /// <summary> /// 分页容器的CSS名称 /// </summary> public string ClassName { get { return string.IsNullOrEmpty(_className) ? "paginator" : _className; }set{ _className = value; } }

private string _currentPageCss; /// <summary> /// 当前页面按钮css /// </summary> public string CurrentPageButtonCss { get { return string.IsNullOrEmpty(_currentPageCss) ? "cpb" : _currentPageCss; }set{ _currentPageCss = value; } }

private bool _showSpanText; /// <summary> /// Span 标签中文字信息是否显示 默认为false不显示 /// </summary> public bool ShowSpanText { get { return (object)_showSpanText == null ? false : _showSpanText; } set { _showSpanText = value; } }

private string _spanTextClass; /// <summary> /// 分页文字描述span标签css /// </summary> public string SpanTextClass { get { return string.IsNullOrEmpty(_spanTextClass) ? "stc" : _spanTextClass; } set { _spanTextClass = value; } }

private string _submitButtonText; /// <summary> /// 确定按钮文字显示 默认显示"确定" /// </summary> public string SubmitButtonText { get { return string.IsNullOrEmpty(_submitButtonText) ? "确定" : _submitButtonText; } set { _submitButtonText = value; } } }分页显示html代码的拼接方法: View Codepublic class SplitManager { public static string AspNetPagers(Pager pager) { StringBuilder sb = new StringBuilder(); string attr=""; int pagecount = 0;//当前页面的总层数 int floorcount = 0;//分页的总层数 int currentLastPage = 0;//当前最后一页的页码 int pageNum = pager.RecordCount / pager.PageSize + 1;//总页数 1~24 sb.AppendFormat("<div class=\"{0}\">\n", pager.ClassName); attr=pager.PageIndex==1?"disabled=disabled":"";//标志当前页第一页是否相等 来控制前俩个按钮的有效性 sb.AppendFormat(GetAHtml(attr,string.Format(pager.URL,1),pager.FirstPageText));//添加最前一页的代码 sb.AppendFormat(GetAHtml(attr, string.Format(pager.URL, pager.PageIndex-1), pager.PrePageText));//添加上一页的代码 pagecount = pager.PageIndex/pager.MaxShowPageSize;//当前页数 0~1~2 pagecount = pager.PageIndex % pager.MaxShowPageSize == 0 ? pagecount - 1 : pagecount;//清除当 当前页数为分页页码数的整数倍页时除数多一的状况 floorcount = pageNum / pager.MaxShowPageSize;//页面层数 0~1~2 currentLastPage=pageNum<pager.MaxShowPageSize * (pagecount + 1)?pageNum:pager.MaxShowPageSize * (pagecount + 1); if (pager.PageIndex > pager.MaxShowPageSize)//当当前序号大于每页页码个数时显示再前端... { sb.AppendFormat(GetAHtml("", string.Format(pager.URL, pager.MaxShowPageSize * pagecount), "...")); } for (int i = pager.MaxShowPageSize * pagecount + 1; i <= currentLastPage; i++) { if (i == pager.PageIndex)//判断循环页面是否为当前页 { sb.AppendFormat(GetSpanHtml(i, pager.CurrentPageButtonCss)); } else { sb.AppendFormat(GetAHtml("", string.Format(pager.URL, i),i.ToString())); } } if (pager.PageIndex <= pager.MaxShowPageSize * floorcount)//当当前序号小于倒数第二页页码时显示在后端... { sb.AppendFormat(GetAHtml("", string.Format(pager.URL, pager.MaxShowPageSize * (pagecount + 1) + 1), "...")); } attr = pager.PageIndex == pageNum ? "disabled=disabled" : "";//标志当前页最后一页是否相等 来控制后俩个按钮的有效性 sb.AppendFormat(GetAHtml(attr, string.Format(pager.URL, pager.PageIndex+1), pager.NextPageText));//添加后一页的代码 sb.AppendFormat(GetAHtml(attr, string.Format(pager.URL, pageNum), pager.LastPageText));//添加最后一页的代码 if (pager.ShowSpanText)//是否显示分页文字span标签显示 { sb.AppendFormat("<span class=\"" + pager.SpanTextClass + "\">共{0}页,每页{1}条记录 \n", pageNum, pager.PageSize); sb.AppendFormat("到第<input type=\"input\" id=\"jumpNum\" style=\"width:20px;\" name=\"jump\" value=\"{0}\" />页", pager.PageIndex == pageNum ? pageNum : pager.PageIndex + 1); sb.AppendFormat("<a href=\"#\" style=\"float:none;\" onclick=\"javascript:jump();\">" + pager.SubmitButtonText + "</a></span>\n"); sb.Append(GetJumpScript(pager.URL));//添加按钮跳转的Javascript代码 } sb.AppendFormat("</div>");// return sb.ToString(); }

/// <summary> /// get the html of a label /// </summary> /// <param name="title">a's title</param> /// <param name="url">the url of a</param> /// <param name="attr">the attribute</param> /// <returns>return html string</returns> private static string GetAHtml(string attr,string url,string title) { return "<a " + attr + " href=\""+url+"\" style=\"margin-right:5px;\">"+title+"</a>\n"; }

/// <summary> /// get the html of a label /// </summary> /// <param name="num">the content of span</param> /// <param name="className">Class style Name</param> /// <returns>return html string </returns> private static string GetSpanHtml(int num, string className) { return "<span class=\"" + className + "\">" + num + "</span>\n"; }

/// <summary&g