asp.net mvc 分页

ExpandedBlockStart.gif 代码
     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();
        }
}


调用这个扩张方法

ExpandedBlockStart.gif 代码
< 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} 格式
// 用于告知提交的目的地


 

ExpandedBlockStart.gif 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是处理用户自定义页面索引


 

 

转载于:https://www.cnblogs.com/jianjialin/archive/2009/12/07/1618595.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值