.NetCore 分页控件实现原理处理以及条件分页处理
说明
自定义一个类继承TagHelper,注意自定义类的 必须以TagHelper结尾,这个有点类是属性 Attribute的写法
protected TagHelper();
//
// 摘要:
// When a set of Microsoft.AspNetCore.Razor.TagHelpers.ITagHelpers are executed,
// their Microsoft.AspNetCore.Razor.TagHelpers.TagHelper.Init(Microsoft.AspNetCore.Razor.TagHelpers.TagHelperContext)'s
// are first invoked in the specified Microsoft.AspNetCore.Razor.TagHelpers.TagHelper.Order;
// then their Microsoft.AspNetCore.Razor.TagHelpers.TagHelper.ProcessAsync(Microsoft.AspNetCore.Razor.TagHelpers.TagHelperContext,Microsoft.AspNetCore.Razor.TagHelpers.TagHelperOutput)'s
// are invoked in the specified Microsoft.AspNetCore.Razor.TagHelpers.TagHelper.Order.
// Lower values are executed first.
//
// 备注:
// Default order is 0.
public virtual int Order { get; }
//
// 摘要:
// Initializes the Microsoft.AspNetCore.Razor.TagHelpers.ITagHelper with the given
// context. Additions to Microsoft.AspNetCore.Razor.TagHelpers.TagHelperContext.Items
// should be done within this method to ensure they're added prior to executing
// the children.
//
// 参数:
// context:
// Contains information associated with the current HTML tag.
//
// 备注:
// When more than one Microsoft.AspNetCore.Razor.TagHelpers.ITagHelper runs on the
// same element, TagHelperOutput.GetChildContentAsync may be invoked prior to Microsoft.AspNetCore.Razor.TagHelpers.TagHelper.ProcessAsync(Microsoft.AspNetCore.Razor.TagHelpers.TagHelperContext,Microsoft.AspNetCore.Razor.TagHelpers.TagHelperOutput).
public virtual void Init(TagHelperContext context);
//
// 摘要:
// Synchronously executes the Microsoft.AspNetCore.Razor.TagHelpers.TagHelper with
// the given context and output.
//
// 参数:
// context:
// Contains information associated with the current HTML tag.
//
// output:
// A stateful HTML element used to generate an HTML tag.
public virtual void Process(TagHelperContext context, TagHelperOutput output);
//
// 摘要:
// Asynchronously executes the Microsoft.AspNetCore.Razor.TagHelpers.TagHelper with
// the given context and output.
//
// 参数:
// context:
// Contains information associated with the current HTML tag.
//
// output:
// A stateful HTML element used to generate an HTML tag.
//
// 返回结果:
// A System.Threading.Tasks.Task that on completion updates the output.
//
// 备注:
// By default this calls into Microsoft.AspNetCore.Razor.TagHelpers.TagHelper.Process(Microsoft.AspNetCore.Razor.TagHelpers.TagHelperContext,Microsoft.AspNetCore.Razor.TagHelpers.TagHelperOutput).
public virtual Task ProcessAsync(TagHelperContext context, TagHelperOutput output);
重写一个 ProcessAsync 这里我以异步为例子
首先说明下分页需要的重要参数 定义一个分页参数类
public class PagerOptions
{ /// <summary>
/// 每页数据条数
/// </summary>
public int PageSize { get; set; }
/// <summary>
/// 当前页码
/// </summary>
public int CurrentPageIndex { get; set; }
/// <summary>
/// 数据总条数
/// </summary>
public int ItemTotal { get; set; }
/// <summary>
/// 总页数
/// </summary>
public int PageTotal
{
get
{
return ItemTotal % PageSize > ? ItemTotal / PageSize + : ItemTotal / PageSize;
}
}
/// <summary>
/// 显示的页面个数(只显示5个页码)
/// </summary>
public int EveryCount { get; set; }
/// <summary>
/// 允许选择页码
/// </summary>
public bool IsSelectPageSize { get; set; }
/// <summary>
/// 每页数据条数范围
/// </summary>
public int[] SelectPageSize { get; set; }
/// <summary>
/// 是否显示转到页码
/// </summary>
public bool IsGoPage { get; set; }
/// <summary>
/// 分页访问地址
/// </summary>
public string PageUri { get; set; }
}
PagerOptions
public class PagerTagHelper : TagHelper
{ public PagerOptions PagerOption { get; set; } public override Task ProcessAsync(TagHelperContext context, TagHelperOutput output)
{ output.TagName = "div";
if (PagerOption.PageSize <= )
{
PagerOption.PageSize = ;
}
if (PagerOption.CurrentPageIndex <= )
{
PagerOption.CurrentPageIndex = ;
}
if (PagerOption.CurrentPageIndex > PagerOption.PageTotal)
{
PagerOption.CurrentPageIndex = PagerOption.PageTotal;
}
if (PagerOption.PageTotal <= )
{
return Task.CompletedTask;
} string ax = PagerOption.PageUri;
//接下来就是拼写html样式而已
return base.ProcessAsync(context, output);
} }
PagerTagHelper
样式具体没有实现值说下原理,添加以上类注意对命名空间的引用,不然是无法编写服务器标签的
找到_ViewImports.cshtml文件 中添加
@addTagHelper "ExpressUser.PagerTagHelper,ExpressUser"
@addTagHelper "ExpressUser.PagerOptions,ExpressUser"
然后在页面上编写pager服务器标签 这是是pager 对应的是类 PagerTagHelper 参数类型 PagerOptions 在 PagerTagHelper 中的变量是 PagerOption 所以这里对应 pager-option

其实就这样简单,当然在代码中可以这样使用 ,如果点击分页按钮要保持查询条件分页,这里就需要获取条件了,这就你是什么方式的请求的了
这里分页我以Get为例子
处理下这个对象
public abstract IQueryCollection Query { get; set; }
或者
public abstract IFormCollection Form { get; set; }
var list = Request.Query.ToList();
string querystring = string.Empty;
foreach (var item in list)
{
querystring += "&" + item.Key + "=" + item.Value;
}
ViewBag.PagerOption = new PagerOptions()
{
ItemTotal = ,
PageUri = Request.Path + (string.IsNullOrEmpty(querystring) ? "" : "?" + querystring.Substring())
};
赋值PagerUri 就行了
下面在回到PagerTagHelper中访问看下

.NetCore 分页控件实现原理处理以及条件分页处理的更多相关文章
- jquery 分页控件(二)
上一章主要是关于分页控件的原理,代码也没有重构.在这一章会附上小插件的下载链接,插件主要就是重构逻辑部分,具体可以下载源文件看下,源代码也有注释.为了测试这个插件是能用的,我弄了个简单的asp.net ...
- jquery 分页控件2
jquery 分页控件(二) 上一章主要是关于分页控件的原理,代码也没有重构.在这一章会附上小插件的下载链接,插件主要就是重构逻辑部分,具体可以下载源文件看下,源代码也有注释.为了测试这个插件是能用的 ...
- uinty3d使用ugui封装一个分页控件
我们在显示数据时有的数据比较多,手机内存有限,我们不可能分配很多的控件来显示这些数据,分页是一个不错的选择.比如玩家交易行.我们现在封装一个自己简单的分页控件来显示玩家交易行. 分页控件的原理其实很简 ...
- 在DevExpress程序中使用Winform分页控件直接录入数据并保存
一般情况下,我们都倾向于使用一个组织比较好的独立界面来录入或者展示相关的数据,这样处理比较规范,也方便显示比较复杂的数据.不过在一些情况下,我们也可能需要直接在GridView表格上直接录入或者修改数 ...
- 基于jquery扩展漂亮的分页控件(ajax)
分页控件式大家在熟悉不过的控件,很多情况下都需要使用到分页控件来完成列表数据加载操作,在很多分页控件中有的编写麻烦,有的应用扩展比较复杂,有的分页控件样式比较丑陋,有的分页控件用户体验操作比较简单等等 ...
- 日积月累系列之分页控件(js源码)
最近开发了一款分页控件,分享给大家. 主要功能和界面介绍 cform分页控件支持服务端分页.客户端分页.数据过滤.数据排序等功能. 源码介绍 cform-pager分页控件主要由三部分组成:css.s ...
- winform 自定义分页控件 及DataGridview数据绑定
分页效果如上图所示,用到的控件均为基本控件 ,其方法如下 右击项目-添加-新建项 选择用户控件 然后在用户控件中拖入所需要的Label,Button,Text 用户控件全部代码: using Syst ...
- 自定义WPF分页控件
一.分页控件功能说明 实现如上图所示的分页控件,需要实现一下几个功能: 可以设置每页能够展示的最大列数(例如每页8列.每页16列等等). 加载的数组总数量超过设置的每页列数后,需分页展示. 可以直接点 ...
- .NetCore 实现分页控件(URL分页)实战
上一篇文章介绍了分页控件的具体实现方式,接下来我们就来做一个分页控件 后台数据处理就过度的介绍,下面针对URL分页中的下面几点做说明: 1.搜索条件的状态保持 2.点击分页需要带上搜索条件 3.页码的 ...
随机推荐
- 安装配置ubuntu的web项目(新)
1.下载jre wget -c javadl.oracle.com/webapps/download/AutoDL?BundleId=211989 -O jre-8u101-linux-i586.ta ...
- MT【171】共轭相随
$\textbf{证明:}$对任意$a,b\in R^+$, $\dfrac{1}{\sqrt{a+2b}}+\dfrac{1}{\sqrt{a+4b}}+\dfrac{1}{\sqrt{a+6b}} ...
- 谈谈Java引用和Threadlocal的那些事
1 背景 某一天在某一个群里面的某个群友突然提出了一个问题:"threadlocal的key是虚引用,那么在threadlocal.get()的时候,发生GC之后,key是否是null?&q ...
- 【BZOJ1053】[HAOI2007]反素数(搜索)
[BZOJ1053][HAOI2007]反素数(搜索) 题面 BZOJ 洛谷 题解 大力猜一下用不了几个质因子,那么随便爆搜一下就好了. #include<iostream> #inclu ...
- UDP ------ UDP Broadcast Address
Related information link : 百度百科---->广播地址 Use restrictions: 1. You can only broadcast on the same ...
- Access时间日期函数大全
这里特别推荐WeekdayName() 函数.MonthName() 函数,将日期转换为中文星期名与月份,如"星期一"."五月"一.Date() 函数.Now( ...
- Linux下使用cron让Python程序持久化运行
正常情况下,一个python程序如果希望实现一直运行,不出错不奔溃是很难的,即使编译为可持续文件也是一样 幸运的是很多需求并不是需要24小时不间断运行,而是每隔一段时间运行一次即可 Linux系统自带 ...
- cin,cout,printf,scanf效率对比
From:http://www.cnblogs.com/killerlegend/p/3918452.html Author:KillerLegend Date:2014.8.17 杭电OJ之3233 ...
- hdu 5181 numbers
http://acm.hdu.edu.cn/showproblem.php?pid=5181 题意: 有一个栈,其中有n个数1~n按顺序依次进入栈顶,在某个时刻弹出. 其中m个限制,形如数字A必须在数 ...
- bzoj千题计划211:bzoj1996: [Hnoi2010]chorus 合唱队
http://www.lydsy.com/JudgeOnline/problem.php?id=1996 f[i][j][0/1] 表示已经排出队形中的[i,j],最后一个插入的人在[i,j]的i或j ...