ASP.NET paging control [closed]
Asked Answered
C

6

14

I'm looking for a decent paging control in ASP.NET, much like the Stackoverflow pager. Can anyone recommend one?

I'd prefer one that didn't use Postback either, just a customisable querystring.

Cossack answered 16/3, 2009 at 23:53 Comment(0)
C
4

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.

Cossack answered 17/3, 2009 at 20:3 Comment(0)
J
11

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 = "&hellip;";
    this.Controls.Add(spacer);
}

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

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

Joplin answered 17/3, 2009 at 6:4 Comment(2)
I've noticed it also does [Prev] 1..2 3 4 5...213 [Next]Cossack
Note: This example produces page links that are 0 based, i.e. p=0 for the first page. The example can be changed if the first page link needs to be 1. Hope that makes sense and this helps!Joplin
C
4

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.

Cossack answered 17/3, 2009 at 20:3 Comment(0)
V
2

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).

Vulgarize answered 16/3, 2009 at 23:58 Comment(0)
G
2

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

Gossoon answered 17/3, 2009 at 0:1 Comment(2)
Thanks for the downvote. Given the age of the question and lack of a comment, I'll assume it's a lame revenge downvote unrelated to the actual answer.Gossoon
I didn't down vote, last time I downvoted was OctoberCossack
L
0

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

Langsyne answered 31/3, 2014 at 3:7 Comment(0)
N
0

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

Neutralize answered 5/9, 2015 at 2:28 Comment(2)
I think you should have a disclaimer to say this is your own project.Jayejaylene
Yes sorry I should have.Neutralize

© 2022 - 2024 — McMap. All rights reserved.