Click here to Skip to main content
65,938 articles
CodeProject is changing. Read more.
Articles / web / ASP.NET

Repeater Paging With last and First Page Button

4.50/5 (6 votes)
17 Feb 2014CPOL 16.6K  
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)