说明

自定义一个类继承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 分页控件实现原理处理以及条件分页处理的更多相关文章

  1. jquery 分页控件(二)

    上一章主要是关于分页控件的原理,代码也没有重构.在这一章会附上小插件的下载链接,插件主要就是重构逻辑部分,具体可以下载源文件看下,源代码也有注释.为了测试这个插件是能用的,我弄了个简单的asp.net ...

  2. jquery 分页控件2

    jquery 分页控件(二) 上一章主要是关于分页控件的原理,代码也没有重构.在这一章会附上小插件的下载链接,插件主要就是重构逻辑部分,具体可以下载源文件看下,源代码也有注释.为了测试这个插件是能用的 ...

  3. uinty3d使用ugui封装一个分页控件

    我们在显示数据时有的数据比较多,手机内存有限,我们不可能分配很多的控件来显示这些数据,分页是一个不错的选择.比如玩家交易行.我们现在封装一个自己简单的分页控件来显示玩家交易行. 分页控件的原理其实很简 ...

  4. 在DevExpress程序中使用Winform分页控件直接录入数据并保存

    一般情况下,我们都倾向于使用一个组织比较好的独立界面来录入或者展示相关的数据,这样处理比较规范,也方便显示比较复杂的数据.不过在一些情况下,我们也可能需要直接在GridView表格上直接录入或者修改数 ...

  5. 基于jquery扩展漂亮的分页控件(ajax)

    分页控件式大家在熟悉不过的控件,很多情况下都需要使用到分页控件来完成列表数据加载操作,在很多分页控件中有的编写麻烦,有的应用扩展比较复杂,有的分页控件样式比较丑陋,有的分页控件用户体验操作比较简单等等 ...

  6. 日积月累系列之分页控件(js源码)

    最近开发了一款分页控件,分享给大家. 主要功能和界面介绍 cform分页控件支持服务端分页.客户端分页.数据过滤.数据排序等功能. 源码介绍 cform-pager分页控件主要由三部分组成:css.s ...

  7. winform 自定义分页控件 及DataGridview数据绑定

    分页效果如上图所示,用到的控件均为基本控件 ,其方法如下 右击项目-添加-新建项 选择用户控件 然后在用户控件中拖入所需要的Label,Button,Text 用户控件全部代码: using Syst ...

  8. 自定义WPF分页控件

    一.分页控件功能说明 实现如上图所示的分页控件,需要实现一下几个功能: 可以设置每页能够展示的最大列数(例如每页8列.每页16列等等). 加载的数组总数量超过设置的每页列数后,需分页展示. 可以直接点 ...

  9. .NetCore 实现分页控件(URL分页)实战

    上一篇文章介绍了分页控件的具体实现方式,接下来我们就来做一个分页控件 后台数据处理就过度的介绍,下面针对URL分页中的下面几点做说明: 1.搜索条件的状态保持 2.点击分页需要带上搜索条件 3.页码的 ...

随机推荐

  1. javascript面向对象系列第五篇——拖拽的实现

    前面的话 在之前的博客中,拖拽的实现使用了面向过程的写法.本文将以面向对象的写法来实现拖拽 写法 <style> .test{height: 50px;width: 50px;backgr ...

  2. Python day7之mysql

    写在前面: 由于毕业论文撰写和答辩耽搁了几个月,但是在这几个月没有放弃学习Python,就是没有时间写博客.进行我们主要对数据库mysql的操作指令集的学习. 一.mysql术语 Mysql是最流行的 ...

  3. Problem C Dist 解题报告

    Problem C Dist Description 有一个\(n\)个点带边权的连通无向图,边集用\(k\)个集合\(s_1,s_2,\dots,s_k\)和\(k\)个整数\(w_1,w_2,\d ...

  4. 01-go语言开始-HelloWorld

    以输出HelloWorld为目标 Go的发展史 Go语言诞生(2007年的谷歌)的背景是由于软件开发的新挑战: 多核硬件架构 超大规模分布式计算集群 Web模式导致的前所未有的开发规模和更新速度 Go ...

  5. Mysql(五) JDBC

    一.JDBC JDBC(Java DataBase Connectivity) 是Java 数据库连接API.    JDBC完成三件事: 与一个数据库连接          向数据库发送SQL语句 ...

  6. jquery生成二维码并实现图片下载

    1.引入jquery的两个js文件 <script src="../scripts/erweima/jquery-1.10.2.min.js"></script& ...

  7. bootstrap 全局 CSS 样式

    http://v3.bootcss.com/css/#less-mixins-utility 深入了解 Bootstrap 底层结构的关键部分,包括我们让 web 开发变得更好.更快.更强壮的最佳实践 ...

  8. Linux上case用法

    Linux上case用法示例: #!/bin/bash # This is a script for test case ASK_COUNT=$ #从参数获取该变量的值 # if [ -z " ...

  9. dedecms添加文章时提示标题为空,编辑文章时编辑器空白的解决办法

    dedecms添加文章时提示标题为空,编辑文章时编辑器空白的解决办法 dedecms出现这个问题与代码无关,主要是和PHP的版本有关,用的PHP5.4,更换成PHP5.2之后就不会有这个问题了. 问题 ...

  10. SQL Server 排名函数( ROW_NUMBER、RANK、DENSE_RANK、NTILE )

    排名函数是Sql Server2005新增的功能,下面简单介绍一下他们各自的用法和区别.我们新建一张Order表并添加一些初始数据方便我们查看效果. CREATE TABLE [dbo].[Order ...