代码
public
static
class
htmlPager
{
public static string myPager( this System.Web.Mvc.HtmlHelper html, int totalPage, int currentPage,
Func < int , string > actionUrls) {
System.Text.StringBuilder sb = new System.Text.StringBuilder();
#region “前一页”
TagBuilder tgPrev = new TagBuilder( " a " );
tgPrev.InnerHtml = " 前一页 " ;
if (currentPage != 1 && totalPage > 1 )
{
tgPrev.MergeAttribute( " href " , actionUrls( 1 ));
}
sb.Append(tgPrev.ToString());
#endregion
if (totalPage <= 10 )
{
for ( int i = 1 ; i <= totalPage; i ++ )
{
TagBuilder tg = new TagBuilder( " a " );
tg.MergeAttribute( " href " , actionUrls(i));
tg.InnerHtml = (i.ToString());
if (i == currentPage)
{
tg.AddCssClass( " selected " );
}
sb.Append(tg.ToString());
}
}
else { // 大于10页
if (currentPage < 7 )
{
for ( int i = 1 ; i <= totalPage; i ++ )
{
TagBuilder tg = new TagBuilder( " a " );
if (i < 8 )
{
tg.MergeAttribute( " href " , actionUrls(i));
tg.InnerHtml = (i.ToString());
}
else if (i == 8 )
tg.InnerHtml = " …… " ;
else {
if (i > (totalPage - 2 )) {
tg.MergeAttribute( " href " , actionUrls(i));
tg.InnerHtml = (i.ToString());
}
}
if (i == currentPage)
{
tg.AddCssClass( " selected " );
}
if (tg.InnerHtml != "" )
sb.Append(tg.ToString());
}
}
else if (currentPage >= 7 ) {
for ( int i = 1 ; i <= totalPage; i ++ )
{
TagBuilder tg = new TagBuilder( " a " );
if (i < 3 ) {
tg.MergeAttribute( " href " , actionUrls(i));
tg.InnerHtml = (i.ToString());
}
if (i == 3 )
tg.InnerHtml = " …… " ;
if ((totalPage - currentPage) > 6 )
{
if ((totalPage - currentPage) > 5 )
{
if ((currentPage - i) <= 2 && (currentPage - i) >= - 3 )
{
tg.MergeAttribute( " href " , actionUrls(i));
tg.InnerHtml = (i.ToString());
}
if ((currentPage - i) == - 4 )
tg.InnerHtml = " …… " ;
if ((totalPage - i) <= 2 )
{
tg.MergeAttribute( " href " , actionUrls(i));
tg.InnerHtml = (i.ToString());
}
}
else if ((currentPage - i) < 2 )
{
tg.MergeAttribute( " href " , actionUrls(i));
tg.InnerHtml = (i.ToString());
}
}
else
{
if ((currentPage - i) < 2 || (totalPage - i) < 7 )
{
tg.MergeAttribute( " href " , actionUrls(i));
tg.InnerHtml = (i.ToString());
}
}
if (i == currentPage)
tg.AddCssClass( " selected " );
if (tg.InnerHtml != "" )
sb.Append(tg.ToString());
}
}
}
#region “后一页”
TagBuilder tgNext = new TagBuilder( " a " );
tgNext.InnerHtml = " 后一页 " ;
if (currentPage != totalPage && totalPage > 1 )
{
tgNext.MergeAttribute( " href " , actionUrls(currentPage + 1 ));
}
sb.Append(tgNext.ToString());
#endregion
#region 搜索
TagBuilder tbTxt = new TagBuilder( " input " );
tbTxt.MergeAttribute( " value " , currentPage.ToString());
tbTxt.MergeAttribute( " id " , " _Ext_txtmyPageIndex " );
tbTxt.MergeAttribute( " name " , " _Ext_txtmyPageIndex " );
tbTxt.MergeAttribute( " style " , " width:50px; " );
sb.Append(tbTxt.ToString());
TagBuilder tbHidden_TotalPages = new TagBuilder( " input " );
tbHidden_TotalPages.MergeAttribute( " value " , totalPage.ToString());
tbHidden_TotalPages.MergeAttribute( " id " , " _Ext_txtmyTotalPage " );
tbHidden_TotalPages.MergeAttribute( " name " , " _Ext_txtmyTotalPage " );
tbHidden_TotalPages.MergeAttribute( " type " , " hidden " );
sb.Append(tbHidden_TotalPages.ToString());
TagBuilder tbGo = new TagBuilder( " input " );
tbGo.MergeAttribute( " type " , " submit " );
tbGo.MergeAttribute( " value " , " GO " );
sb.Append(tbGo.ToString());
#endregion
return sb.ToString();
}
}
{
public static string myPager( this System.Web.Mvc.HtmlHelper html, int totalPage, int currentPage,
Func < int , string > actionUrls) {
System.Text.StringBuilder sb = new System.Text.StringBuilder();
#region “前一页”
TagBuilder tgPrev = new TagBuilder( " a " );
tgPrev.InnerHtml = " 前一页 " ;
if (currentPage != 1 && totalPage > 1 )
{
tgPrev.MergeAttribute( " href " , actionUrls( 1 ));
}
sb.Append(tgPrev.ToString());
#endregion
if (totalPage <= 10 )
{
for ( int i = 1 ; i <= totalPage; i ++ )
{
TagBuilder tg = new TagBuilder( " a " );
tg.MergeAttribute( " href " , actionUrls(i));
tg.InnerHtml = (i.ToString());
if (i == currentPage)
{
tg.AddCssClass( " selected " );
}
sb.Append(tg.ToString());
}
}
else { // 大于10页
if (currentPage < 7 )
{
for ( int i = 1 ; i <= totalPage; i ++ )
{
TagBuilder tg = new TagBuilder( " a " );
if (i < 8 )
{
tg.MergeAttribute( " href " , actionUrls(i));
tg.InnerHtml = (i.ToString());
}
else if (i == 8 )
tg.InnerHtml = " …… " ;
else {
if (i > (totalPage - 2 )) {
tg.MergeAttribute( " href " , actionUrls(i));
tg.InnerHtml = (i.ToString());
}
}
if (i == currentPage)
{
tg.AddCssClass( " selected " );
}
if (tg.InnerHtml != "" )
sb.Append(tg.ToString());
}
}
else if (currentPage >= 7 ) {
for ( int i = 1 ; i <= totalPage; i ++ )
{
TagBuilder tg = new TagBuilder( " a " );
if (i < 3 ) {
tg.MergeAttribute( " href " , actionUrls(i));
tg.InnerHtml = (i.ToString());
}
if (i == 3 )
tg.InnerHtml = " …… " ;
if ((totalPage - currentPage) > 6 )
{
if ((totalPage - currentPage) > 5 )
{
if ((currentPage - i) <= 2 && (currentPage - i) >= - 3 )
{
tg.MergeAttribute( " href " , actionUrls(i));
tg.InnerHtml = (i.ToString());
}
if ((currentPage - i) == - 4 )
tg.InnerHtml = " …… " ;
if ((totalPage - i) <= 2 )
{
tg.MergeAttribute( " href " , actionUrls(i));
tg.InnerHtml = (i.ToString());
}
}
else if ((currentPage - i) < 2 )
{
tg.MergeAttribute( " href " , actionUrls(i));
tg.InnerHtml = (i.ToString());
}
}
else
{
if ((currentPage - i) < 2 || (totalPage - i) < 7 )
{
tg.MergeAttribute( " href " , actionUrls(i));
tg.InnerHtml = (i.ToString());
}
}
if (i == currentPage)
tg.AddCssClass( " selected " );
if (tg.InnerHtml != "" )
sb.Append(tg.ToString());
}
}
}
#region “后一页”
TagBuilder tgNext = new TagBuilder( " a " );
tgNext.InnerHtml = " 后一页 " ;
if (currentPage != totalPage && totalPage > 1 )
{
tgNext.MergeAttribute( " href " , actionUrls(currentPage + 1 ));
}
sb.Append(tgNext.ToString());
#endregion
#region 搜索
TagBuilder tbTxt = new TagBuilder( " input " );
tbTxt.MergeAttribute( " value " , currentPage.ToString());
tbTxt.MergeAttribute( " id " , " _Ext_txtmyPageIndex " );
tbTxt.MergeAttribute( " name " , " _Ext_txtmyPageIndex " );
tbTxt.MergeAttribute( " style " , " width:50px; " );
sb.Append(tbTxt.ToString());
TagBuilder tbHidden_TotalPages = new TagBuilder( " input " );
tbHidden_TotalPages.MergeAttribute( " value " , totalPage.ToString());
tbHidden_TotalPages.MergeAttribute( " id " , " _Ext_txtmyTotalPage " );
tbHidden_TotalPages.MergeAttribute( " name " , " _Ext_txtmyTotalPage " );
tbHidden_TotalPages.MergeAttribute( " type " , " hidden " );
sb.Append(tbHidden_TotalPages.ToString());
TagBuilder tbGo = new TagBuilder( " input " );
tbGo.MergeAttribute( " type " , " submit " );
tbGo.MergeAttribute( " value " , " GO " );
sb.Append(tbGo.ToString());
#endregion
return sb.ToString();
}
}
调用这个扩张方法
代码
<
div
class
="pager"
>
<% using (Html.BeginForm())
{ %>
<% = Html.myPager(( int )ViewData[ " totalPage " ],
( int )ViewData[ " currentPage " ], x => Url.Action( " index " , new { id = x })) %>
<% } %>
</ div >
//表达式,也可以不用表达式。
// Url.Action("index", new { id = x })返回 /{controller}/{action}/{id} 格式
// 用于告知提交的目的地
<% using (Html.BeginForm())
{ %>
<% = Html.myPager(( int )ViewData[ " totalPage " ],
( int )ViewData[ " currentPage " ], x => Url.Action( " index " , new { id = x })) %>
<% } %>
</ div >
//表达式,也可以不用表达式。
// Url.Action("index", new { id = x })返回 /{controller}/{action}/{id} 格式
// 用于告知提交的目的地
Colltroller内的方法
private
readonly
int
pagesize
=
5
;
public ActionResult Index( int ? id,FormCollection form)
{
int Ind = 0 ;
if (id == null )
Ind = 1 ;
else
{
string index = form.Get( " _Ext_txtmyPageIndex " );
Ind = (( int )id) == - 1 ? int .Parse(index) : ( int )id; // (int)id;
}
NorthwindEntities ne = new NorthwindEntities();
ViewData[ " currentPage " ] = Ind;
ViewData[ " totalPage " ] = ( int )Math.Ceiling(( double )ne.Products.Count() / pagesize);
return View(ne.Products.OrderBy(f => f.ProductID).Skip(pagesize * (Ind - 1 )).Take(pagesize));
}
[AcceptVerbs(HttpVerbs.Post)]
public ActionResult Index(FormCollection form) {
return RedirectToAction( " index " , new { id = form.Get( " _Ext_txtmyPageIndex " ) });
}
post是处理用户自定义页面索引
public ActionResult Index( int ? id,FormCollection form)
{
int Ind = 0 ;
if (id == null )
Ind = 1 ;
else
{
string index = form.Get( " _Ext_txtmyPageIndex " );
Ind = (( int )id) == - 1 ? int .Parse(index) : ( int )id; // (int)id;
}
NorthwindEntities ne = new NorthwindEntities();
ViewData[ " currentPage " ] = Ind;
ViewData[ " totalPage " ] = ( int )Math.Ceiling(( double )ne.Products.Count() / pagesize);
return View(ne.Products.OrderBy(f => f.ProductID).Skip(pagesize * (Ind - 1 )).Take(pagesize));
}
[AcceptVerbs(HttpVerbs.Post)]
public ActionResult Index(FormCollection form) {
return RedirectToAction( " index " , new { id = form.Get( " _Ext_txtmyPageIndex " ) });
}
post是处理用户自定义页面索引