Click here to Skip to main content
12,396,461 members (35,080 online)
Click here to Skip to main content
Add your own
alternative version

Tagged as

Stats

14.2K views
3 bookmarked
Posted

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

You may also be interested in...

Comments and Discussions

 
AnswerFixed Code Pin
SlimSjakie14-Oct-14 23:15
memberSlimSjakie14-Oct-14 23:15 
Questionnext and previous links disappear Pin
Rajarya_2229-Jul-14 9:24
memberRajarya_2229-Jul-14 9:24 
QuestionRequire one information Pin
rmad199021-Jun-13 6:05
memberrmad199021-Jun-13 6:05 
QuestionGetting Error Pin
Member 100421399-May-13 23:38
memberMember 100421399-May-13 23:38 
GeneralMy vote of 5 Pin
liltium17-Oct-12 21:52
memberliltium17-Oct-12 21:52 
GeneralMy vote of 3 Pin
DrABELL21-Sep-12 5:38
memberDrABELL21-Sep-12 5:38 
GeneralRe: My vote of 3 Pin
togoa21-Sep-12 5:52
membertogoa21-Sep-12 5:52 
GeneralRe: My vote of 3 Pin
DrABELL21-Sep-12 6:04
memberDrABELL21-Sep-12 6:04 
GeneralRe: My vote of 3 Pin
togoa23-Sep-12 19:57
membertogoa23-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.

| Advertise | Privacy | Terms of Use | Mobile
Web01 | 2.8.160721.1 | Last Updated 21 Sep 2012
Article Copyright 2012 by togoa
Everything else Copyright © CodeProject, 1999-2016
Layout: fixed | fluid