之前写了一个关于ASP.NET MVC+妹子UI的Ajax分页控件,今天在那个控件的基础上,抽出了一个后台的分页控件,特此与大家分享
public class PageFactory
{
public PageFactory( string Controller, string Action, int Total, int limit,string Area=""
,Dictionary<string,string> Parameter=null)
{
this._controller = Controller;
this._action = Action;
this._area = Area;
Url = "/" + _area + "/"+this._controller + "/" + this._action + "?"+_assemblyParameter(Parameter)+"offset=";
_totalBars = Convert.ToInt32(Math.Ceiling((double)(Total *1.0f/ limit)));
}
public PageFactory(string Controller, string Action, int Total, int limit,string Area="")
{
this._controller = Controller;
this._action = Action;
Url = "/" + this._controller + "/" + this._action + "?offset=";
_totalBars = Convert.ToInt32(Math.Ceiling((double)(Total*1.0f / limit)));
}
private string _area;
private string _action;
private string _controller;
/// <summary>
/// 增加参数方法
/// </summary>
/// <param name="Parameter"></param>
/// <returns></returns>
private string _assemblyParameter(Dictionary<string, string> Parameter)
{
if (Parameter == null || Parameter.Count == 0)
{
return "";
}
StringBuilder assem = new StringBuilder();
foreach (var item in Parameter)
{
assem.Append(item.Key + "=" + item.Value+"&");
}
return assem.ToString();
}
private string Url;
private int _totalBars;
/// <summary>
/// 初始化方法
/// </summary>
/// <returns></returns>
private MvcHtmlString _init()
{
StringBuilder htmlSb = new StringBuilder();
htmlSb.Append("<li class=\"am-disabled\"><a href=\"javascript:;\">«</a></li>");
//上一页,(禁用的效果)
string nextBar = this._totalBars > 1 ? "<li><a href=\"" + this.Url + 2 + "\">»</a></li>" : "<li class=\"am-disabled\"><a href=\"javascript:;\">»</a></li>";
//最后一页
htmlSb.Append("<li class=\"am-active\"><a href=\"" + Url + 1 + "\">1</a></li>");
if (_totalBars <= 7)
{
for (var i = 1; i < _totalBars; i++)
{
htmlSb.Append("<li><a href=\"" + Url + (i + 1) + "\">" + (i + 1) + "</a></li>");
}
}
else
{
for (var j = 1; j < 5; j++)
{
htmlSb.Append("<li><a href=\"" + Url + (j + 1) + "\">" + (j + 1) + "</a></li>");
}
htmlSb.Append("<li><a href=\"javascript:;\">...</a></li>");
htmlSb.Append("<li><a href=\"" + Url + (_totalBars) + "\">" + (_totalBars) + "</a></li>");
}
htmlSb.Append(nextBar);
return new MvcHtmlString(htmlSb.ToString());
}
/// <summary>
/// 核心方法
/// </summary>
/// <param name="offset"></param>
/// <returns></returns>
public MvcHtmlString Core(int offset)
{
if (offset == 0)
{
return this._init();
}
StringBuilder htmlHelper = new StringBuilder();
var nextBar = offset == this._totalBars ? "<li class=\"am-disabled\"><a href=\"javascript:;\">»</a></li>" :
"<li><a href=\"" + this.Url + (offset + 1) + "\">»</a></li>";
var preBar = offset == 1 ? "<li class=\"am-disabled\"><a href=\"javascript:;\">«</a></li>" :
"<li><a href=\"" + this.Url + (offset - 1) + "\">«</a></li>";
//组装向上一个节点和下一页节点
htmlHelper.Append(preBar);
try
{
#region 分页核心代码
if (this._totalBars > 7)
{
if (offset < 5)
{
for (var i = 1; i <= 5; i++)
{
if (i == offset)
{
htmlHelper.Append("<li class=\"am-active\"><a href=\"" + this.Url + offset + "\">" + offset + "</a></li>");
}
else
{
htmlHelper.Append("<li><a href=\"" + this.Url + i + "\">" + i + "</a></li>");
}
}
htmlHelper.Append("<li><a>...</a></li>");
htmlHelper.Append("<li><a href=\"" + this.Url + (this._totalBars) + "\">" + (this._totalBars) + "</a></li>" + nextBar);
}
else if (offset >= 5 && offset <= this._totalBars - 4)
{
htmlHelper.Append("<li><a href=\"" + this.Url + 1 + "\">" + 1 + "</a></li>");
//第一个
htmlHelper.Append("<li><a>...</a></li>"); //省略号
htmlHelper.Append("<li><a href=\"" + this.Url + (offset - 1) + "\">" + (offset - 1) + "</a></li>");
htmlHelper.Append("<li class=\"am-active\"><a href=\"" + this.Url + offset + "\">" + offset + "</a></li>");
htmlHelper.Append("<li><a href=\"" + this.Url + (offset + 1) + "\">" + (offset + 1) + "</a></li>");
htmlHelper.Append("<li><a>...</a></li>"); //省略号;
htmlHelper.Append("<li><a href=\"" + this.Url + (this._totalBars) + "\">" + (this._totalBars) + "</a></li>"); //尾页
htmlHelper.Append(nextBar);
}
else if (offset > this._totalBars - 4 && offset <= this._totalBars)
{
htmlHelper.Append("<li><a href=\"" + this.Url + 1 + "\">" + 1 + "</a></li>" + "<li><a>...</a></li>");
for (var j = 4; j >= 0; j--)
{
if (this._totalBars - j == offset)
{
htmlHelper.Append("<li class=\"am-active\"><a href=\"" + this.Url + (this._totalBars - j) + "\">" + (this._totalBars - j) + "</a></li>");
}
else
{
htmlHelper.Append("<li><a href=\"" + this.Url + (this._totalBars - j) + "\">" + (this._totalBars - j) + "</a></li>");
}
}
htmlHelper.Append(nextBar);
}
else
{
throw new Exception("分页数据出错!");
}
}
else
{
for (var i = 1; i <= this._totalBars; i++)
{
if (i == offset)
{
htmlHelper.Append("<li class=\"am-active\"><a href=\"" + this.Url + offset + "\">" + offset + "</a></li>");
}
else
{
htmlHelper.Append("<li><a href=\"" + this.Url + i + "\">" + i + "</a></li>");
}
}
htmlHelper.Append(nextBar);
}
#endregion
}
catch (Exception ex)
{
return new MvcHtmlString("");
}
return new MvcHtmlString(htmlHelper.ToString());
}
}
以上就是分页的核心代码,看不懂没有关系,会调用就行。
为了使得后台调用简单,我们将其扩展在Razor的Html对象上,那样的话,@Html(参数列表)就可以使用了
public static MvcHtmlString PageBar(this HtmlHelper htmlhelper, string area, string controller, string action, int pagesize, int totalcount)
{
int offset = 0;
string queryOffset=HttpContext.Current.Request.QueryString["offset"];
int.TryParse(queryOffset, out offset);
PageFactory helper = null;
Dictionary<string, string> dic = new Dictionary<string, string>();
dic.Add("Name", "Yangxu");
dic.Add("Product", "Fruit");
helper = new PageFactory(controller, action, totalcount, pagesize,area,dic);
return helper.Core(offset);
}
因为有些时候要GET传递参数,需要传入GET参数的键值对。
运行结果如下
可供参考的CSS样式我也为大家贴出来:
.result-page a {
display:inline-block;
float: left;
margin-left: 10px;
width: 30px;
height: 30px;
line-height: 30px;
text-align: center;
font-family: '微软雅黑';
border: #cccccc 1px solid;
border-radius: 2px;
color: #31a030;
background: white;
margin-top: 35px;
text-decoration:none;
}
.result-page a:hover,.result-page a:active {
background: #31a030;
color: white;
}
.am-active a{
background: #31a030 !important;
color: white !important;
}
希望对大家有帮助。