ASP.NET 分页控件

发布于 2024-07-15 02:36:11 字数 1542 浏览 10 评论 0原文

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(6

还在原地等你 2024-07-22 02:36:11

自己动手非常容易。 我创建了一个基于堆栈溢出分页器的简单用户控件,具有两个属性...

  1. 根据基础数据可用的页面总数
  2. 要显示的链接数量

通过读取查询字符串确定所选页面。 最大的挑战是使用新页码更改 URL。 此方法使用查询字符串参数“p”来指定要显示的页面...

string getLink(int toPage)
{
    NameValueCollection query = HttpUtility.ParseQueryString(Request.Url.Query);
    query["p"] = toPage.ToString();

    string url = Request.Path;

    for(int i = 0; i < query.Count; i++)
    {
        url += string.Format("{0}{1}={2}", 
            i == 0 ? "?" : "&", 
            query.Keys[i], 
            string.Join(",", query.GetValues(i)));
    }

    return url;
}

一个简单的公式来确定要显示的页码范围...

int min = Math.Min(Math.Max(0, Selected - (PageLinksToShow / 2)), Math.Max(0, PageCount - PageLinksToShow + 1));
int max = Math.Min(PageCount, min + PageLinksToShow);

然后使用类似的内容生成每个链接(其中 min 和 max 指定范围要创建的页面链接)...

for (int i = min; i <= max; i++)
{
    HyperLink btn = new HyperLink();
    btn.Text = (i + 1).ToString();
    btn.NavigateUrl = getLink(i);
    btn.CssClass = "pageNumbers" + (Selected == i ? " current" : string.Empty);
    this.Controls.Add(btn);
}

还可以创建“上一个”(和“下一个”)按钮...

HyperLink previous = new HyperLink();
previous.Text = "Previous";
previous.NavigateUrl = getLink(Selected - 1);

第一个和最后一个按钮很简单...

HyperLink previous = new HyperLink();
previous.Text = "1";
first.NavigateUrl = getLink(0);

在确定何时显示“...”时,显示当链接范围不在第一页或最后一页旁边时的文字控制...

if (min > 0)
{
    Literal spacer = new Literal();
    spacer.Text = "…";
    this.Controls.Add(spacer);
}

对上面的“max < PageCount”执行相同的操作。

所有这些代码都放在 CreateChildControls 的重写方法中。

It's quite easy to roll your own. I created a simple user control based on the stack overflow pager with two properties...

  1. Total number of pages available according to the underlying data
  2. Number of links to show

The selected page is determined by reading the query string. The biggest challenge was altering the URL with the new page number. This method uses a query string parameter 'p' to specify which page to display...

string getLink(int toPage)
{
    NameValueCollection query = HttpUtility.ParseQueryString(Request.Url.Query);
    query["p"] = toPage.ToString();

    string url = Request.Path;

    for(int i = 0; i < query.Count; i++)
    {
        url += string.Format("{0}{1}={2}", 
            i == 0 ? "?" : "&", 
            query.Keys[i], 
            string.Join(",", query.GetValues(i)));
    }

    return url;
}

A simple formula to determine the range of page numbers to show...

int min = Math.Min(Math.Max(0, Selected - (PageLinksToShow / 2)), Math.Max(0, PageCount - PageLinksToShow + 1));
int max = Math.Min(PageCount, min + PageLinksToShow);

Each link then gets generated using something like (where min and max specify the range of page links to create)...

for (int i = min; i <= max; i++)
{
    HyperLink btn = new HyperLink();
    btn.Text = (i + 1).ToString();
    btn.NavigateUrl = getLink(i);
    btn.CssClass = "pageNumbers" + (Selected == i ? " current" : string.Empty);
    this.Controls.Add(btn);
}

One can also create 'Previous' (and 'Next') buttons...

HyperLink previous = new HyperLink();
previous.Text = "Previous";
previous.NavigateUrl = getLink(Selected - 1);

The first and last buttons are straight forward...

HyperLink previous = new HyperLink();
previous.Text = "1";
first.NavigateUrl = getLink(0);

In determining when to show the "...", show a literal control when the link range is not next to the first or last pages...

if (min > 0)
{
    Literal spacer = new Literal();
    spacer.Text = "…";
    this.Controls.Add(spacer);
}

Do the same for above for "max < PageCount".

All of this code is put in an override method of CreateChildControls.

最冷一天 2024-07-22 02:36:11

我期待更多的答案,但看起来很多人只是自己做。 我发现了一个不错的,经常在 codeproject.com

cp

它与 stackoverflow.com 的不太一样。 如果有一个像样的开源控件具有多种不同的输出选项,那就太好了。

I was expecting more answers but it looks like a lot of people just make their own. I've found a decent one that is maintained quite often on codeproject.com

cp

It's not quite the same as the stackoverflow.com one. It'd be nice if there was a decent open source control that had a variety of different output options.

2024-07-22 02:36:11

我使用过 DevExpress 和 Telerik 页面控件,并且更喜欢 DevExpress 分页器。 我不确定 DevExpress 寻呼机是否可以直接使用查询字符串,但如果不能,我会感到惊讶,因为它非常灵活。 至于下载后现有页面之间的分页,所有内容都可以驻留在客户端上,或者如果需要访问服务器,则该控件完全配备 AJAX。 我建议您从 www.devexpress.com 开始搜索,然后也查看 www.Telerik.com(也配备了 AJAX)。

I've worked with the DevExpress and Telerik page controls and prefer the DevExpress pager. I'm not sure if the DevExpress pager can work directly with a querystring but I would be surprised if it didn't as it is very flexible. As far as paging between existing pages after download, everything can reside on the client or, if a trip to the server is necessary, the control is fully AJAX equipped. I suggest you start your search at www.devexpress.com and then check out www.Telerik.com as well (which is also AJAX equipped).

巨坚强 2024-07-22 02:36:11

不是控件,但这是在数据库级别实现分页的方法: SQL Server 2005 分页

Not a control, but this is the way to implement paging at the DB level: SQL Server 2005 Paging

可遇━不可求 2024-07-22 02:36:11

我编写了一个寻呼机控件,名为:Flexy Pager
了解更多: http://www.codeproject .com/Articles/748270/Flexy-Pager-for-ASP-NET-WebForm-MVC

I have written a pager control named: Flexy Pager
Read more: http://www.codeproject.com/Articles/748270/Flexy-Pager-for-ASP-NET-WebForm-MVC

enter image description here

堇色安年 2024-07-22 02:36:11

您可以尝试 NPager。 使用页面索引的查询字符串,无回发。 需要 Bootstrap 进行样式设置,但是您可以使用“分页”CSS 类为控件拥有自己的自定义 css 类。这是一个有效的 演示

在此处输入图像描述

You can try NPager. Uses query string for page indexes, no postbacks. Needs Bootstrap for styling, however you can have your own custom css classes for the control using 'pagination' CSS class.Here is a working DEMO

enter image description here

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文