个人写的分页方法

/**分页**/

#PageConDiv{padding:10px; margin:0px auto; text-align:right; }
#PageConDiv span a,#PageConDiv span span{ display:inline-block;text-align:center;height:20px; padding:0 8px;vertical-align:middle;line-height:20px;text-decoration:none; /*color:#8195b6;background:#e9f5e7;*/font-size:12px; margin-right:5px; color:#000; border:solid 1px #ddd; background:#fafafa;}
#PageConDiv span .SelPageA{ color:#5f7a5b; border:solid 1px #5f7a5b; background:#EBEEF6; font-weight:bold;}
#PageConDiv span .PrvA{}
#PageConDiv span .NextA{}
#PageConDiv span .GotoSpan{ padding:0px; margin-right:0px; border-right:none;}
#PageConDiv span #Gotobox{width:25px;height:14px;border:solid 1px #ccc; margin:0px;padding:0px; margin:2px; margin-bottom:0px;}
#PageConDiv span #GotoBtn{}

      
/// <summary> /// 分页页码生成 /// </summary> /// <param name="CurrPage">当前页码</param> /// <param name="PageCount">总页数</param> /// <param name="Params">页面参数 &param1=value&param2=value样式</param> /// <returns></returns> protected string PagerControl(int CurrPage, int PageCount,string Params) { System.Text.StringBuilder PageStr = new System.Text.StringBuilder(); //分页生成字符 CurrPage = CurrPage <= PageCount && CurrPage > 0 ? CurrPage : CurrPage > 0 ? PageCount : 1; //处理错误输入 PageStr.Append("<span>"); if (PageCount > 1) { string DirectUrl = Request.Url.LocalPath.ToString();//页面地址 int BeginIndex, EndIndex; // 首页|上一页 if (CurrPage > 1) { PageStr.Append("<a href=\"" + DirectUrl + "?page=1" + Params + " \">首页</a>"); PageStr.Append("<a class='PrvA' href=\"" + DirectUrl + "?page=" + (CurrPage - 1) + Params + " \">上一页</a>"); } #region 初始页码计算 if (PageCount > 10) { if (CurrPage > 6) { if (!(PageCount < (CurrPage + 4))) { BeginIndex = CurrPage - 5; EndIndex = CurrPage + 4; } else { BeginIndex = PageCount - 9; EndIndex = PageCount; } } else { BeginIndex = 1; EndIndex = 10; } } else { BeginIndex = 1; EndIndex = PageCount; } #endregion #region 页码生成 for (int i = BeginIndex; i < EndIndex + 1; i++) { PageStr.Append("<a " + (i != CurrPage ? "" : "class=\"SelPageA\"") + " href=\"" + DirectUrl + "?page=" + i + Params + "\">" + i + "</a>"); } if (PageCount > 10 && PageCount - CurrPage > 4) { PageStr.Append("<a href=\"" + DirectUrl + "?page=" + PageCount + Params + "\">..." + PageCount + "</a>"); } #endregion // 下一页|尾页 if (CurrPage < PageCount) { PageStr.Append("<a class='NextA' href=\"" + DirectUrl + "?page=" + (CurrPage + 1) + Params + " \">下一页</a>"); } // 跳转页 if (PageCount > 5) { PageStr.Append("<span class=\"GotoSpan\"><input type='text' id='Gotobox' /></span>"); PageStr.Append("<a id=\"GotoBtn\" href=\"" + DirectUrl + "?page=" + PageCount + Params + " \">跳</a>"); } } PageStr.Append("</span>"); return PageStr.ToString(); }
posted @ 2012-07-10 08:18  he1595  阅读(287)  评论(0编辑  收藏  举报