Click here to Skip to main content
Click here to Skip to main content

The easiest way to implement NumericNextPrev paging of GridView

, 21 Sep 2012 CPOL
Rate this:
Please Sign up or sign in to vote.
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:  

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)

Share

About the Author

togoa
Web Developer GrapeCity
Russian Federation Russian Federation
No Biography provided

Comments and Discussions

 
AnswerFixed Code PinmemberSlimSjakie14-Oct-14 23:15 
Questionnext and previous links disappear PinmemberRajarya_2229-Jul-14 9:24 
QuestionRequire one information Pinmemberrmad199021-Jun-13 6:05 
QuestionGetting Error PinmemberMember 100421399-May-13 23:38 
GeneralMy vote of 5 Pinmemberliltium17-Oct-12 21:52 
GeneralMy vote of 3 PinmemberDrABELL21-Sep-12 5:38 
GeneralRe: My vote of 3 Pinmembertogoa21-Sep-12 5:52 
GeneralRe: My vote of 3 PinmemberDrABELL21-Sep-12 6:04 
GeneralRe: My vote of 3 Pinmembertogoa23-Sep-12 19:57 

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

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

| Advertise | Privacy | Mobile
Web02 | 2.8.141015.1 | Last Updated 21 Sep 2012
Article Copyright 2012 by togoa
Everything else Copyright © CodeProject, 1999-2014
Terms of Service
Layout: fixed | fluid