Click here to Skip to main content
15,881,881 members
Articles / Web Development / ASP.NET
Tip/Trick

The easiest way to implement NumericNextPrev paging of GridView

Rate me:
Please Sign up or sign in to vote.
3.54/5 (5 votes)
21 Sep 2012CPOL 24.4K   3   10
How to get > in your GridView pager in one step. One C# method. No code download needed.

Introduction

GridView has no built-in pager mode that allows you to display both next/previous page links as well as page numbers. I have no idea why. But it's really easy to implement.

Background 

Once I had requirement to do this, I've found several solutions over the network but all of them were to complex. Creating several files, catching bugs, injecting code at several points of control flow... uff, why so difficult? GridView control has already the possibility to display page numbers and ALL that I need was just add "<<" and ">>" buttons! If this is your case feel free to use my code.

Using the code 

0. Implement common paging using "Numeric" type of pager. My code expects pager in the bottom position but it's easy to make changes if you want to use it on top. 

1. Your GridView should bind the "DataBound" event: OnDataBound="gv_DataBound"

2. Use this code:  

C#
protected void gv_DataBound(object sender, EventArgs e)
{
    EnableNextPrevNavigationForNumericPagedGrid(gv);
}

private static void EnableNextPrevNavigationForNumericPagedGrid(GridView gv)
{
    if (gv.BottomPagerRow == null)
        return;
    Table pagerTable = (Table)gv.BottomPagerRow.Controls[0].Controls[0];

    bool prevAdded = false;
    if (gv.PageIndex != 0)
    {
        TableCell prevCell = new TableCell();
        LinkButton prevLink = new LinkButton
                              {
                                  Text = "<<",
                                CommandName = "Page",
                                CommandArgument = ((LinkButton)pagerTable.Rows[0].Cells[gv.PageIndex - 1].Controls[0]).CommandArgument
                              };
        prevCell.Controls.Add(prevLink);
        pagerTable.Rows[0].Cells.AddAt(0, prevCell);
        prevAdded = true;
    }

    if (gv.PageIndex != gv.PageCount - 1)
    {
        TableCell nextCell = new TableCell();
        LinkButton nextLink = new LinkButton
                              {
                                  Text = ">>",
                                CommandName = "Page",
                                CommandArgument = ((LinkButton)pagerTable.Rows[0].Cells[gv.PageIndex + 
                                  (prevAdded ? 2 : 1)].Controls[0]).CommandArgument
                              };
        nextCell.Controls.Add(nextLink);
        pagerTable.Rows[0].Cells.Add(nextCell);
    }
}

Paging works in normal way. The only thing that my code does is adding "<<" and ">>" buttons. "<<" button won't appear at the first page as well as ">>" at the last one.  

License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)


Written By
Web Developer GrapeCity
Russian Federation Russian Federation
This member has not yet provided a Biography. Assume it's interesting and varied, and probably something to do with programming.

Comments and Discussions

 
QuestionImplementing first and Last Pin
Adeel6882-Jun-17 23:23
Adeel6882-Jun-17 23:23 
AnswerFixed Code Pin
Christian Nateghi14-Oct-14 23:15
Christian Nateghi14-Oct-14 23:15 
Questionnext and previous links disappear Pin
Rajarya_2229-Jul-14 9:24
Rajarya_2229-Jul-14 9:24 
QuestionRequire one information Pin
rmad199021-Jun-13 6:05
rmad199021-Jun-13 6:05 
QuestionGetting Error Pin
Member 100421399-May-13 23:38
Member 100421399-May-13 23:38 
GeneralMy vote of 5 Pin
liltium17-Oct-12 21:52
liltium17-Oct-12 21:52 
Thank you, this is brilliant. Just what i was looking for. Something simple and easy.
GeneralMy vote of 3 Pin
DrABELL21-Sep-12 5:38
DrABELL21-Sep-12 5:38 
GeneralRe: My vote of 3 Pin
togoa21-Sep-12 5:52
togoa21-Sep-12 5:52 
GeneralRe: My vote of 3 Pin
DrABELL21-Sep-12 6:04
DrABELL21-Sep-12 6:04 
GeneralRe: My vote of 3 Pin
togoa23-Sep-12 19:57
togoa23-Sep-12 19:57 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.