Click here to Skip to main content
15,889,867 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
I am working on repeater custom paging. The paging is working fine and it looks like this:
First Prev 1 2 3 4 5 6 7 8 Next Last
As my records increases it also increases the page numbers, if i have 500 records then it will not good see numbers from 1 to 50.

I need to work like this:
First Previous 1 2 3 4 5 Next Last
Only five records show at a time, If i am at page5 and click on next then it should be look like this 6 7 8 9 10 and when i am at page2 and click next then it should be look like this 1 2 3 4 5
C#
protected int BindRepeaterData()
    {
        con.Open();
        SqlCommand cmd = new SqlCommand("sps_searchresult", con);
        cmd.CommandType = CommandType.StoredProcedure;
        cmd.Parameters.AddWithValue("@country", 1);
        cmd.Parameters.AddWithValue("@state", state);
        cmd.Parameters.AddWithValue("@pincode", pincode);
        cmd.Parameters.AddWithValue("@category", category);
        cmd.Parameters.AddWithValue("@subcategory", subcategory);
        con.Close();
        
        //save the result in data table
        DataTable dt = new DataTable();
        SqlDataAdapter adp = new SqlDataAdapter(cmd);
        adp.Fill(dt);
        int noOfRows = dt.Rows.Count;
        lbl_totalrecords.Text = "Total Results Found : " + noOfRows.ToString();
        PagedDataSource pgitems = new PagedDataSource();
        DataView dv = new DataView(dt);
        pgitems.DataSource = dv;
        pgitems.AllowPaging = true;
        pgitems.PageSize = 2;
        int count = pgitems.PageCount;
        pgitems.CurrentPageIndex = PageNumber;
        if (pgitems.Count > 0)
        {
            lbtnPrev.Visible = true;
            lbtnNext.Visible = true;
            lbtnFirst.Visible = true;
            lbtnLast.Visible = true;

            //Change the text Now viewing text
            lblCurrentPage.Text = "Page : " + (PageNumber + 1).ToString() + " of " + pgitems.PageCount.ToString();

            rptPaging.Visible = true;
            ArrayList pages = new ArrayList();
            for (int i = 0; i < pgitems.PageCount; i++)
                pages.Add((i + 1).ToString());
            rptPaging.DataSource = pages;
            rptPaging.DataBind();
        }
        else
        {
            lbtnPrev.Visible = false;
            lbtnNext.Visible = false;
            lbtnFirst.Visible = false;
            lbtnLast.Visible = false;
            rptPaging.Visible = false;
        }

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

        rep_results.DataSource = pgitems;
        rep_results.DataBind();

        return count;
        
    }

protected void rptPaging_ItemCommand(object source, RepeaterCommandEventArgs e)
    {
        PageNumber = Convert.ToInt32(e.CommandArgument) - 1;
        BindRepeaterData();
    }
public int PageNumber
    {
        get
        {
            //get current page number
            object obj = this.ViewState["_PageNumber"];

            if (obj == null)
            {
                return 0;
            }
            else
            {
                return (int)obj;
            }
        }
        set
        {
            //set in viewstate the current page number
            this.ViewState["_PageNumber"] = value;
        }
    }
protected void lbtnPrev_Click(object sender, EventArgs e)
    {
        PageNumber -= 1;
        BindRepeaterData();
    }
protected void lbtnNext_Click(object sender, EventArgs e)
    {
        PageNumber += 1;
        BindRepeaterData();
    }
protected void lbtnFirst_Click(object sender, EventArgs e)
    {
        PageNumber = 0;
        BindRepeaterData();
    }
protected void lbtnLast_Click(object sender, EventArgs e)
    {
        PageNumber = BindRepeaterData() - 1;
        BindRepeaterData();
    }
<pre lang="HTML">
<div style="overflow: hidden;">
             <asp:Repeater ID="rptPaging" runat="server" onitemcommand="rptPaging_ItemCommand" >
                <ItemTemplate>
                <asp:LinkButton ID="btnPage" style="padding:8px; margin:2px; background:#911216; text-decoration:none; border:solid 1px black; font:8pt tahoma;"
                CommandName="Page" CommandArgument="<%# Container.DataItem %>" runat="server" ForeColor="White" Font-Bold="True"><%# Container.DataItem %>
                </asp:LinkButton>
                </ItemTemplate>
            </asp:Repeater>
            </div>
            <br/>
            
                <asp:Button ID="lbtnFirst" runat="server" Text="First"  onclick="lbtnFirst_Click" Width="70"></asp:Button>
                <asp:Button ID="lbtnPrev" runat="server" Text="Prev" onclick="lbtnPrev_Click" Width="70"></asp:Button>
                <asp:Label ID="lblCurrentPage" runat="server" Text=""></asp:Label>
                <asp:Button ID="lbtnNext" runat="server" Width="70" Text="Next" onclick="lbtnNext_Click"></asp:Button>                
                <asp:Button ID="lbtnLast" runat="server" Width="70" Text="Last" onclick="lbtnLast_Click"></asp:Button>
Posted
Updated 16-May-14 4:15am
v2

1 solution

Please replace the following code in BindRepeaterData function

C#
rptPaging.Visible = true;
ArrayList pages = new ArrayList();
for (int i = 0; i < pgitems.PageCount; i++)
    pages.Add((i + 1).ToString());
rptPaging.DataSource = pages;
rptPaging.DataBind();

with
C#
rptPaging.Visible = true;

// Here we calculate the Start Index and End Index of the page numbers that we need to display in pagination..
int iStartIndex = (pgitems.CurrentPageIndex/5)*5+1;
int iEndIndex = iStartIndex+4;
if(iEndIndex > pgitems.PageCount-1)
{
  iEndIndex = pgitems.PageCount-1;
}

ArrayList pages = new ArrayList();
for (int i = iStartIndex; i <= iEndIndex ; i++)
    pages.Add((i + 1).ToString());
rptPaging.DataSource = pages;
rptPaging.DataBind();


I hope this will help you... :)
 
Share this answer
 
Comments
Raj Negi 16-May-14 15:51pm    
perfect :)

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



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900