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

Repeater Paging With last and First Page Button

Rate me:
Please Sign up or sign in to vote.
4.50/5 (6 votes)
17 Feb 2014CPOL 16.2K   8   3
This tip provides the repeter paging with First and Last Page button

Introduction

Repeater paging provides the Paging facility in Repeater which was not default given into Repeater control of the ASP.NET.

Background

This includes the Four Linkbuttons, the names of which are First, Previous, Next, Last respectively and give the paging as per your page requirement like display 5, 10, 15... as you need.

Using the Code  

For using this one, you need to integrate this code in your CS file and add this function, then call it wherever you need to display your data.

You have to put four link buttons between your Repeater <separatortemplate> tag.

C++
    /// <summary>
    /// ////////////////////////////////////////////////////////////////
    /// </summary>
    public int CurrentPage
    {
        get
        {
            //get current page number
            object obj = this.ViewState["_CurrentPage"];

            if (obj == null)
            {
                return 0;
            }
            else
            {
                return (int)obj;
            }
        }
        set
        {
            //set in viewstate the current page number
            this.ViewState["_CurrentPage"] = value;
        }
    }

    public int DisplayRepeter()
    {
        SqlParameter[] p = new SqlParameter[1];
        p[0] = new SqlParameter("projectid", drpprojectlist.SelectedValue);
        DataSet ds = cm.SP_Select("SP_StudentSelectByProject", p);
        rptstudnet.DataSource = ds;
        rptstudnet.DataBind();

        PagedDataSource pds = new PagedDataSource();

        pds.DataSource = ds.Tables[0].DefaultView;
        pds.AllowPaging = true;
        pds.PageSize = 6;
        int count = pds.PageCount;

        pds.CurrentPageIndex = CurrentPage;

        if (pds.Count > 0)
        {
            lbtnPrev.Visible = true;
            lbtnNext.Visible = true;
            lbtnFirst.Visible = true;
            lbtnLast.Visible = true;

            lblStatus.Text = "Page " + Convert.ToString
            (CurrentPage + 1) + "of" + Convert.ToString(pds.PageCount);
        }
        else
        {
            lbtnPrev.Visible = false;
            lbtnNext.Visible = false;
            lbtnFirst.Visible = false;
            lbtnLast.Visible = false;
        }

        lbtnPrev.Enabled = !pds.IsFirstPage;
        lbtnNext.Enabled = !pds.IsLastPage;
        lbtnFirst.Enabled = !pds.IsFirstPage;
        lbtnLast.Enabled = !pds.IsLastPage;

        rptstudnet.DataSource = pds;
        rptstudnet.DataBind();

        return count;
    }

    protected void lbtnPrev_Click(object sender, EventArgs e)
    {
        CurrentPage -= 1;
        DisplayRepeter();
    }

    protected void lbtnNext_Click(object sender, EventArgs e)
    {
        CurrentPage += 1;
        DisplayRepeter();
    }

    protected void lbtnFirst_Click(object sender, EventArgs e)
    {
        CurrentPage = 0;
        DisplayRepeter();
    }

    protected void lbtnLast_Click(object sender, EventArgs e)
    {
        CurrentPage = DisplayRepeter() - 1;
        DisplayRepeter();
    }
    /// <summary>
    /// /////////////////////////////////////////////////////////
    /// </summary>  

Add this link button in your source code of .ASPX page between repeater separatortemplate.

ASP.NET
<li><asp:Button ID="lbtnFirst" runat="server" 
    class="btn btn-primary" Text="First"  onclick="lbtnFirst_Click" 
    Width="70"></asp:Button></li>
<li><asp:Button ID="lbtnPrev" runat="server" 
class="btn btn-primary" Text="Prev" 
onclick="lbtnPrev_Click" Width="70"></asp:Button></li>
<li><asp:Button ID="lbtnNext" runat="server" 
class="btn btn-primary" Width="70" Text="Next" 
onclick="lbtnNext_Click"></asp:Button></li>
<li><asp:Button ID="lbtnLast" runat="server" 
class="btn btn-primary" Width="70" Text="Last" 
onclick="lbtnLast_Click"></asp:Button></li>  

Made changes in the above code as per your requirements like changing the StoreProcedure, etc.

License

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


Written By
Software Developer (Junior) THOMSON REUTERS
India India
This member has not yet provided a Biography. Assume it's interesting and varied, and probably something to do with programming.

Comments and Discussions

 
Questionplease provide some image type description Pin
Er. A.K. Singh18-Feb-14 20:29
professionalEr. A.K. Singh18-Feb-14 20:29 
AnswerRe: please provide some image type description Pin
JatinKhimani23-Feb-14 18:25
JatinKhimani23-Feb-14 18:25 
GeneralRe: please provide some image type description Pin
Nelek26-Feb-14 21:53
protectorNelek26-Feb-14 21:53 
Yes, we can do it, but if you are posting something and people ask you to add something to make it more understable.... then you should add it. After all, you are the one posting the tip and the one who is explaining it.

Imagine you are at school, the teacher explains something, you say you don't understand it and ask for an example, and then the teacher tells you.... Oh, it's simple, you can do this and see it by yourself. D'Oh! | :doh:
M.D.V. Wink | ;)

If something has a solution... Why do we have to worry about?. If it has no solution... For what reason do we have to worry about?
Help me to understand what I'm saying, and I'll explain it better to you
Rating helpful answers is nice, but saying thanks can be even nicer.

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.