zoukankan      html  css  js  c++  java
  • asp.net mvc 分页

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


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


  • 相关阅读:
    VIM 用正则表达式,非贪婪匹配,匹配竖杠,竖线, 匹配中文,中文正则,倒数第二列, 匹配任意一个字符 :
    中国科学院图书馆分类法
    让进程在后台可靠运行的几种方法 nohup,setsid,&,disown,CTRL-z ,screen
    Exception Handling Statements (C# Reference)
    ChannelFactory.Endpoint 上的地址属性为空。ChannelFactory 的终结点必须指定一个有效的地址。
    .NET中六个重要的概念:栈、堆、值类型、引用类型、装箱和拆箱
    WCF Host中的BaseAddress 和 Endpoint中的Address的区别
    使用vs自带的wcf配置工具
    Automatic Code Generation-->Implement Interface
    Learning WCF Chapter1 Exposing Multiple Service Endpoints
  • 原文地址:https://www.cnblogs.com/jianjialin/p/1618595.html
Copyright © 2011-2022 走看看