制作了个简单的ASP.MVC分页控件

作者:互联网   出处:控件中国网   2014-11-05 19:17:14   阅读:1

asp.net mvc 1.0正式版发布之后,试用mvc写了几个简单的小项目,由于每个项目都免不了要用到数据的分页功能,

所以决定把自己经常使用的分页相关的功能封装下,做成一个HtmlHelper的扩展方法,以方便自己和像我一样刚接触MVC的

新手使用。

 

分页功能实现方面参考了网上的一些内容,加上自己的一些修改,使用方式如下: 

(鉴于分页代码基本上和别人的没什么两样,所以这里就省略了。)

 


 public static string Pager(this HtmlHelper html, IPageData model, bool alwaysShow,bool showNumber)
        {
            
string text = "<!--没有任何数据,所以不显示分页控件-->";
            
bool show = model.TotalCount > 0 && alwaysShow || model.TotalCount > model.PageSize;
            
if (show)
            {
                StringBuilder sb 
= new StringBuilder();
                var reqUrl 
= html.ViewContext.RequestContext.HttpContext.Request.Url.ToString();
                
string link = "";
                
if (reqUrl.IndexOf("page">= 0)
                {
                    Regex re 
= new Regex(@"page=(\d+)", RegexOptions.IgnoreCase);
                    link 
= re.Replace(reqUrl, "page=%7Bpage%7D");
                }
                
else if (reqUrl.IndexOf("?"< 0)
                {
                    link 
= reqUrl + "?page=%7Bpage%7D";
                }
                
else
                {
                    link 
= reqUrl + "&page=%7Bpage%7D";
                }

                
#region 生成四个按钮

                TagBuilder root 
= new TagBuilder("div");
                root.AddCssClass(
"ZuiePager");                

                TagBuilder fBtn 
= new TagBuilder("a");
                fBtn.AddCssClass(
"btn");
                fBtn.MergeAttribute(
"style""font-family:Webdings;");
                fBtn.InnerHtml 
= "9";

                TagBuilder pBtn 
= new TagBuilder("a");
                pBtn.AddCssClass(
"btn");
                pBtn.MergeAttribute(
"style""font-family:Webdings;");
                pBtn.InnerHtml 
= "3";

                TagBuilder nBtn 
= new TagBuilder("a");
                nBtn.AddCssClass(
"btn");
                nBtn.MergeAttribute(
"style""font-family:Webdings;");
                nBtn.InnerHtml 
= "4";

                TagBuilder lBtn 
= new TagBuilder("a");
                lBtn.AddCssClass(
"btn");
                lBtn.MergeAttribute(
"style""font-family:Webdings;");
                lBtn.InnerHtml 
= ":";

                
#endregion

                
int p = model.PageIndex;
                
#region 判断首尾页
                
if (model.HasPreviousPage)
                {
                    
string pUrl = link.Replace("%7Bpage%7D", (p - 1).ToString());
                    
string fUrl = link.Replace("%7Bpage%7D", (1).ToString());
                    fBtn.MergeAttribute(
"href", fUrl);
                    pBtn.MergeAttribute(
"href", pUrl);
                }
                
else
                {
                    fBtn.MergeAttribute(
"disabled""disabled");
                    pBtn.MergeAttribute(
"disabled""disabled");
                }

                
if (model.HasNextPage)
                {
                    
string nUrl = link.Replace("%7Bpage%7D", (p + 1).ToString());
                    
string lUrl = link.Replace("%7Bpage%7D", model.TotalPages.ToString());
                    nBtn.MergeAttribute(
"href", nUrl);
                    lBtn.MergeAttribute(
"href", lUrl);
                }
                
else
                {
                    nBtn.MergeAttribute(
"disabled""disabled");
                    lBtn.MergeAttribute(
"disabled""disabled");
                }

                
#endregion

                sb.Append(fBtn.ToString());
                sb.Append(pBtn.ToString());

                
#region 生成中间数字
                
if (showNumber)
                {
                    
int begin = p - 5;
                    
int end = p + 5;
                    
if (begin < 1)
                    {
                        begin 
= 1;
                        end 
= 11;
                    }
                    
if (end > model.TotalPages)
                    {
                        end 
= model.TotalPages;
                        begin 
= end > 11 ? end - 10 : 1;
                    }

                    
for (int i = begin; i <= end; i++)
                    {
                        TagBuilder num 
= new TagBuilder("span");
                        
if (p == i)
                        {
                            num.AddCssClass(
"current");
                            num.InnerHtml 
= i.ToString();
                        }
                        
else
                        {
                            
string url = link.Replace("%7Bpage%7D", i.ToString());
                            num.AddCssClass(
"num");
                            TagBuilder a 
= new TagBuilder("a");
                            a.MergeAttribute(
"href", url);
                            a.InnerHtml 
= i.ToString();
                            num.InnerHtml 
= a.ToString()+" ";
                        }
                        sb.Append(num.ToString());
                    }
                }
                
#endregion

                sb.Append(nBtn.ToString());
                sb.Append(lBtn.ToString());

                
#region 生成页信息
                TagBuilder index 
= new TagBuilder("span");
                index.AddCssClass(
"num");
                index.InnerHtml 
= p.ToString();

                TagBuilder pagecount 
= new TagBuilder("span");
                pagecount.AddCssClass(
"num");
                pagecount.InnerHtml 
= model.TotalPages.ToString();

                TagBuilder pagesize 
= new TagBuilder("span");
                pagesize.AddCssClass(
"num");
                pagesize.InnerHtml 
= model.PageSize.ToString();

                TagBuilder count 
= new TagBuilder("span");
                count.AddCssClass(
"num");
                count.InnerHtml 
= model.TotalCount.ToString();

                sb.Append(
string.Format(" 第{0}页/共{1}页 每页{2}条记录 合计{3}条", index.ToString(), pagecount.ToString(), pagesize.ToString(), count.ToString()));

                root.InnerHtml 
= sb.ToString();
                text 
= root.ToString();

                
#endregion
            }
            
return text;
        }

 

 

 
    public class HomeController : Controller
    {
        
public ActionResult Index(int? page)
        {
            List
<Test> list = new List<Test>();
            
for (int i = 1; i <= 25; i++)
            {
                list.Add(
new Test() { Id = i, Title = "测试类标题" + i });
            }

            
//关键代码
             var plist = list.ToPagedList(page, 10);          
            
return View(plist);
        }
    }

 

 


<%@ Page Language="C#" MasterPageFile="~/Views/Shared/Site.Master" Inherits="System.Web.Mvc.ViewPage<PagedList<Test>>" %>
<asp:Content ID="indexContent" ContentPlaceHolderID="MainContent" runat="server">
    
<ul>
    
<%foreach (var test in Model)
      { 
%>
      
<li>[<%=test.Id %>]  <%=test.Title %></li>
    
<%%>
    
</ul>
    
<%=Html.Pager(Model) %>    
</asp:Content>
Copyright© 2006-2015 ComponentCN.com all rights reserved.重庆磐岩科技有限公司(控件中国网) 版权所有 渝ICP备12000264号 法律顾问:元炳律师事务所
客服软件
live chat